#This file runs the analyses focused on STM for the manuscript "Changing Partisan Stereotypes in the Trump Era"#
#Finalized on XX XXXX 2023

####Commands to read in the raw data and run the STM steps####
#These are the commands that we used to shift through and select a model. they can be
#followed to generate the same kinds of results that we used; however, they are 
#time-intensive. We would recommend  working with the data objects generated by
#the topic modeling commands rather than re-creating the topic models from scratch.

#A note in the R script indicates where to pick up with the saved RData object for 
#the analyses presented in the main paper

library(stm)
##Load in and prep the raw dataset (wide format)##
#Change this location to where your files are locally
data1 <- read.csv("Lucid data 2021.csv")
data1.1<- read.csv("Dynata data 2016.csv")

#This combines the datasets into a single object for the STM procedures
data=rbind(data1, data1.1)

#Removing cases with missing data (stm won't work otherwise)
data2 <- data[complete.cases(data$inc),]
data3 <- data2[complete.cases(data2$gender),]
data4 <- data3[complete.cases(data3$white),]
data5 <- data4[complete.cases(data4$interest),]
data7 <- data5[complete.cases(data5$hisp2),]
data8 <- data7[complete.cases(data7$age),]
data9 <- data8[complete.cases(data8$pk_scale),]
data10 <- data9[complete.cases(data9$dem),]
data11 <- data10[complete.cases(data10$rep),]
data12 <- data11[complete.cases(data11$ind),]
data13 <- data12[complete.cases(data12$education2),]
data14 <- data13[which(data11$inc>0),]
data14 <- data14[complete.cases(data14$year2021),]

##STM commands begin here##

##STM MODEL SEARCH##
##PREPPING THE TEXTS##
##DEMOCRATS##
#Prepping text
processedD <- textProcessor(data14$GenD, metadata = data14)
outD <- prepDocuments(processedD$documents, processedD$vocab,
                      processedD$meta, lower.thresh = 5)
docsD <- outD$documents
vocabD <- outD$vocab
metaD <- outD$meta

##REPUBLICANS##
processedR <- textProcessor(data14$GenR, metadata = data14)
outR <- prepDocuments(processedR$documents, processedR$vocab,
                      processedR$meta, lower.thresh = 5)
docsR <- outR$documents
vocabR <- outR$vocab
metaR <- outR$meta

##WIDE MODEL SEARCH##
##DEMOCRATS##
#Wide model search
GenDSearch_alt <- searchK(outD$documents, outD$vocab, K = c(5,10,15,20,25,35,45,55,75),
                          prevalence =~ inc + gender + white + interest + education2 + 
                            hisp2 + age + pk_scale + dem + rep + year2021+
                            dem*year2021+rep*year2021,
                          data = metaD, seed = 1, LDAbeta=FALSE)
knitr::kable(GenDSearch_alt$results)
plot(GenDSearch_alt)

##REPUBLICANS##
#Wide model search
GenRSearch_alt <- searchK(outR$documents, outR$vocab, K = c(5,10,15,20,25,35,45,55,75),
                          prevalence =~ inc + gender + white + interest + education2 + 
                            hisp2 + age + pk_scale + dem + rep + year2021+
                            dem*year2021+rep*year2021,
                          data = metaR, seed = 1, LDAbeta=FALSE)
knitr::kable(GenRSearch_alt$result)
plot(GenRSearch_alt)

##Narrow model search##
##DEMOCRATS##
GenDSearch2 <- searchK(outD$documents, outD$vocab, K = c(2:25),
                       prevalence =~ inc + gender + white + interest + education2 + 
                         hisp2 + age + pk_scale + dem + rep + year2021+
                         dem*year2021+rep*year2021,
                       data = metaD, seed = 1, LDAbeta=FALSE)
knitr::kable(GenDSearch2$results)
plot(GenDSearch2)

##REPUBLICANS##
GenRSearch2 <- searchK(outR$documents, outR$vocab, K = c(2:25),
                       prevalence =~ inc + gender + white + interest + education2 + 
                         hisp2 + age + pk_scale + dem + rep + year2021+
                         dem*year2021+rep*year2021,
                       data = metaR, seed = 1, LDAbeta=FALSE)
knitr::kable(GenRSearch2$results)
plot(GenRSearch2)

GenRSearch2.2 <- searchK(outR$documents, outR$vocab, K = c(2, 4:30),
                         prevalence =~ inc + gender + white + interest + education2 + 
                           hisp2 + age + pk_scale + dem + rep + year2021+
                           dem*year2021+rep*year2021,
                         data = metaR, seed = 1, LDAbeta=FALSE)
knitr::kable(GenRSearch2.2$results)
plot(GenRSearch2.2)

##Close look at models for both parties##
#Sage model selection for Democrats
GenDSelect4Sage <- selectModel(outD$documents, outD$vocab, K = 4,
                               prevalence =~ inc + gender + white + interest + education2 + 
                                 hisp2 + age + pk_scale + dem + rep + year2021+
                                 dem*year2021+rep*year2021,
                               max.em.its = 500,
                               data = outD$meta, runs = 30, seed = 1, LDAbeta=FALSE)

GenDSelect5Sage <- selectModel(outD$documents, outD$vocab, K = 5,
                               prevalence =~ inc + gender + white + interest + education2 + 
                                 hisp2 + age + pk_scale + dem + rep + year2021+
                                 dem*year2021+rep*year2021,
                               max.em.its = 500,
                               data = outD$meta, runs = 30, seed = 1, LDAbeta=FALSE)

GenDSelect6Sage <- selectModel(outD$documents, outD$vocab, K = 6,
                               prevalence =~ inc + gender + white + interest + education2 + 
                                 hisp2 + age + pk_scale + dem + rep + year2021+
                                 dem*year2021+rep*year2021,
                               max.em.its = 500,
                               data = outD$meta, runs = 30, seed = 1, LDAbeta=FALSE)

GenDSelect7Sage <- selectModel(outD$documents, outD$vocab, K = 7,
                               prevalence =~ inc + gender + white + interest + education2 + 
                                 hisp2 + age + pk_scale + dem + rep + year2021+
                                 dem*year2021+rep*year2021,
                               max.em.its = 500,
                               data = outD$meta, runs = 30, seed = 1, LDAbeta=FALSE)


GenDSelect8Sage <- selectModel(outD$documents, outD$vocab, K = 8,
                               prevalence =~ inc + gender + white + interest + education2 + 
                                 hisp2 + age + pk_scale + dem + rep + year2021+
                                 dem*year2021+rep*year2021,
                               max.em.its = 500,
                               data = outD$meta, runs = 30, seed = 1, LDAbeta=FALSE)

GenDSelect9Sage <- selectModel(outD$documents, outD$vocab, K = 9,
                               prevalence =~ inc + gender + white + interest + education2 + 
                                 hisp2 + age + pk_scale + dem + rep + year2021+
                                 dem*year2021+rep*year2021,
                               max.em.its = 500,
                               data = outD$meta, runs = 30, seed = 1, LDAbeta=FALSE)


GenDSelect10Sage <- selectModel(outD$documents, outD$vocab, K = 10,
                                prevalence =~ inc + gender + white + interest + education2 + 
                                  hisp2 + age + pk_scale + dem + rep + year2021+
                                  dem*year2021+rep*year2021,
                                max.em.its = 500,
                                data = outD$meta, runs = 30, seed = 1, LDAbeta=FALSE)

GenDSelect11Sage <- selectModel(outD$documents, outD$vocab, K = 11,
                                prevalence =~ inc + gender + white + interest + education2 + 
                                  hisp2 + age + pk_scale + dem + rep + year2021+
                                  dem*year2021+rep*year2021,
                                max.em.its = 500,
                                data = outD$meta, runs = 30, seed = 1, LDAbeta=FALSE)

GenDSelect12Sage <- selectModel(outD$documents, outD$vocab, K = 12,
                                prevalence =~ inc + gender + white + interest + education2 + 
                                  hisp2 + age + pk_scale + dem + rep + year2021+
                                  dem*year2021+rep*year2021,
                                max.em.its = 500,
                                data = outD$meta, runs = 30, seed = 1, LDAbeta=FALSE)

GenDSelect13Sage <- selectModel(outD$documents, outD$vocab, K = 13,
                                prevalence =~ inc + gender + white + interest + education2 + 
                                  hisp2 + age + pk_scale + dem + rep + year2021+
                                  dem*year2021+rep*year2021,
                                max.em.its = 500,
                                data = outD$meta, runs = 30, seed = 1, LDAbeta=FALSE)

GenDSelect14Sage <- selectModel(outD$documents, outD$vocab, K = 14,
                                prevalence =~ inc + gender + white + interest + education2 + 
                                  hisp2 + age + pk_scale + dem + rep + year2021+
                                  dem*year2021+rep*year2021,
                                max.em.its = 500,
                                data = outD$meta, runs = 30, seed = 1, LDAbeta=FALSE)

GenDSelect15Sage <- selectModel(outD$documents, outD$vocab, K = 15,
                                prevalence =~ inc + gender + white + interest + education2 + 
                                  hisp2 + age + pk_scale + dem + rep + year2021+
                                  dem*year2021+rep*year2021,
                                max.em.its = 500,
                                data = outD$meta, runs = 30, seed = 1, LDAbeta=FALSE)

#Sage model selection for Republicans
GenRSelect4Sage <- selectModel(outR$documents, outR$vocab, K = 4,
                               prevalence =~ inc + gender + white + interest + education2 + 
                                 hisp2 + age + pk_scale + dem + rep + year2021+
                                 dem*year2021+rep*year2021,
                               max.em.its = 500,
                               data = outR$meta, runs = 30, seed = 1, LDAbeta=FALSE)
GenRSelect5Sage <- selectModel(outR$documents, outR$vocab, K = 5,
                               prevalence =~ inc + gender + white + interest + education2 + 
                                 hisp2 + age + pk_scale + dem + rep + year2021+
                                 dem*year2021+rep*year2021,
                               max.em.its = 500,
                               data = outR$meta, runs = 30, seed = 1, LDAbeta=FALSE)
GenRSelect6Sage <- selectModel(outR$documents, outR$vocab, K = 6,
                               prevalence =~ inc + gender + white + interest + education2 + 
                                 hisp2 + age + pk_scale + dem + rep + year2021+
                                 dem*year2021+rep*year2021,
                               max.em.its = 500,
                               data = outR$meta, runs = 30, seed = 1, LDAbeta=FALSE)
GenRSelect7Sage <- selectModel(outR$documents, outR$vocab, K = 7,
                               prevalence =~ inc + gender + white + interest + education2 + 
                                 hisp2 + age + pk_scale + dem + rep + year2021+
                                 dem*year2021+rep*year2021,
                               max.em.its = 500,
                               data = outR$meta, runs = 30, seed = 1, LDAbeta=FALSE)
GenRSelect8Sage <- selectModel(outR$documents, outR$vocab, K = 8,
                               prevalence =~ inc + gender + white + interest + education2 + 
                                 hisp2 + age + pk_scale + dem + rep + year2021+
                                 dem*year2021+rep*year2021,
                               max.em.its = 500,
                               data = outR$meta, runs = 30, seed = 1, LDAbeta=FALSE)
GenRSelect9Sage <- selectModel(outR$documents, outR$vocab, K = 9,
                               prevalence =~ inc + gender + white + interest + education2 + 
                                 hisp2 + age + pk_scale + dem + rep + year2021+
                                 dem*year2021+rep*year2021,
                               max.em.its = 500,
                               data = outR$meta, runs = 30, seed = 1, LDAbeta=FALSE)
GenRSelect10Sage <- selectModel(outR$documents, outR$vocab, K = 10,
                                prevalence =~ inc + gender + white + interest + education2 + 
                                  hisp2 + age + pk_scale + dem + rep + year2021+
                                  dem*year2021+rep*year2021,
                                max.em.its = 500,
                                data = outR$meta, runs = 30, seed = 1, LDAbeta=FALSE)
GenRSelect11Sage <- selectModel(outR$documents, outR$vocab, K = 11,
                                prevalence =~ inc + gender + white + interest + education2 + 
                                  hisp2 + age + pk_scale + dem + rep + year2021+
                                  dem*year2021+rep*year2021,
                                max.em.its = 500,
                                data = outR$meta, runs = 30, seed = 1, LDAbeta=FALSE)
GenRSelect12Sage <- selectModel(outR$documents, outR$vocab, K = 12,
                                prevalence =~ inc + gender + white + interest + education2 + 
                                  hisp2 + age + pk_scale + dem + rep + year2021+
                                  dem*year2021+rep*year2021,
                                max.em.its = 500,
                                data = outR$meta, runs = 30, seed = 1, LDAbeta=FALSE)
GenRSelect13Sage <- selectModel(outR$documents, outR$vocab, K = 13,
                                prevalence =~ inc + gender + white + interest + education2 + 
                                  hisp2 + age + pk_scale + dem + rep + year2021+
                                  dem*year2021+rep*year2021,
                                max.em.its = 500,
                                data = outR$meta, runs = 30, seed = 1, LDAbeta=FALSE)

GenRSelect14Sage <- selectModel(outR$documents, outR$vocab, K = 14,
                                prevalence =~ inc + gender + white + interest + education2 + 
                                  hisp2 + age + pk_scale + dem + rep + year2021+
                                  dem*year2021+rep*year2021,
                                max.em.its = 500,
                                data = outR$meta, runs = 30, seed = 1, LDAbeta=FALSE)
GenRSelect15Sage <- selectModel(outR$documents, outR$vocab, K = 15,
                                prevalence =~ inc + gender + white + interest + education2 + 
                                  hisp2 + age + pk_scale + dem + rep + year2021+
                                  dem*year2021+rep*year2021,
                                max.em.its = 500,
                                data = outR$meta, runs = 30, seed = 1, LDAbeta=FALSE)
#Plot the results
plotModels(GenDSelect5Sage)
plotModels(GenDSelect7Sage)
plotModels(GenDSelect8Sage)
plotModels(GenDSelect9Sage)
plotModels(GenDSelect10Sage)

plotModels(GenRSelect4Sage)
plotModels(GenRSelect7Sage)
plotModels(GenRSelect8Sage)
plotModels(GenRSelect9Sage)
plotModels(GenRSelect11Sage)

summary(GenDSelect5Sage$runout[[1]])
summary(GenRSelect5Sage$runout[[1]])

##SELECT BEST MODELS OF EACH KIND##
#CHOSEN DEM MODELS
#Five topic model
GenDChoice5.5 <- GenDSelect5Sage$runout[[5]]
summary(GenDChoice5.2)

GenDChoice5.5 <- GenDSelect5Sage$runout[[5]]
summary(GenDChoice5.5)

GenDChoice5.6 <- GenDSelect5Sage$runout[[6]]
summary(GenDChoice5.6)

#Seven topic model
GenDChoice7.2 <- GenDSelect7Sage$runout[[2]]
summary(GenDChoice7.2)

GenDChoice7.3 <- GenDSelect7Sage$runout[[3]]
summary(GenDChoice7.3)

GenDChoice7.5 <- GenDSelect7Sage$runout[[5]]
summary(GenDChoice7.5)

#8 topic model
plotModels(GenDSelect8Sage)
GenDChoice8.1 <- GenDSelect8Sage$runout[[1]]
summary(GenDChoice8.1)

GenDChoice8.6 <- GenDSelect8Sage$runout[[6]]
summary(GenDChoice8.6)


#Nine topic model
GenDChoice9.3 <- GenDSelect9Sage$runout[[3]]
summary(GenDChoice9.3)

GenDChoice9.5 <- GenDSelect9Sage$runout[[5]]
summary(GenDChoice9.5)

#Ten topic model
GenDChoice10.1 <- GenDSelect10Sage$runout[[1]]
summary(GenDChoice10.1)

#CHOSEN REP MODEL
#Four topic model
GenRChoice4.2 <- GenRSelect4Sage$runout[[2]]
summary(GenRChoice4.2)

GenRChoice4.6 <- GenRSelect4Sage$runout[[6]]
summary(GenRChoice4.6)

#Seven topic model
GenRChoice7.2 <- GenRSelect7Sage$runout[[2]]
summary(GenRChoice7.2)

GenRChoice7.3 <- GenRSelect7Sage$runout[[3]]
summary(GenRChoice7.3)

GenRChoice7.5 <- GenRSelect7Sage$runout[[5]]
summary(GenRChoice7.5)

#Eight topic model
GenRChoice8.5 <- GenRSelect8Sage$runout[[5]]
summary(GenRChoice8.5)

GenRChoice8.1 <- GenRSelect8Sage$runout[[1]]
summary(GenRChoice8.1)

#Nine topic model
GenRChoice9.5 <- GenRSelect9Sage$runout[[5]]
summary(GenRChoice9.5)

#eleven topic model
GenRChoice11.1 <- GenRSelect11Sage$runout[[1]]
summary(GenRChoice11.1)

GenRChoice11.2 <- GenRSelect11Sage$runout[[2]]
summary(GenRChoice11.2)

GenRChoice11.3 <- GenRSelect11Sage$runout[[3]]
summary(GenRChoice11.3)


##CONSIDER EXEMPLAR TOPICS##
###REPUBLICANS##
##4.6 model##
plot(GenRChoice4.6, type="summary")
thoughtR4.6.1 <- findThoughts(GenRChoice4.6, texts=outR$meta$GenR, n=10, topics=1)$docs[[1]]
plotQuote(thoughtR4.6.1, main="Rep 4 topics, Topic 1")
thoughtR4.6.2 <- findThoughts(GenRChoice4.6, texts=outR$meta$GenR, n=10, topics=2)$docs[[1]]
plotQuote(thoughtR4.6.2, main="Rep 4 topics, Topic 2")
thoughtR4.6.3 <- findThoughts(GenRChoice4.6, texts=outR$meta$GenR, n=10, topics=3)$docs[[1]]
plotQuote(thoughtR4.6.3, main="Rep 4 topics, Topic 3")
thoughtR4.6.4 <- findThoughts(GenRChoice4.6, texts=outR$meta$GenR, n=10, topics=4)$docs[[1]]
plotQuote(thoughtR4.6.4, main="Rep 4 topics, Topic 4")

##7.2 model##
plot(GenRChoice7.2, type="summary")
thoughtR7.2.1 <- findThoughts(GenRChoice7.2, texts=outR$meta$GenR, n=10, topics=1)$docs[[1]]
plotQuote(thoughtR7.2.1, main="Rep 7 topics, Topic 1")
thoughtR7.2.2 <- findThoughts(GenRChoice7.2, texts=outR$meta$GenR, n=10, topics=2)$docs[[1]]
plotQuote(thoughtR7.2.2, main="Rep 7 topics, Topic 2")
thoughtR7.2.3 <- findThoughts(GenRChoice7.2, texts=outR$meta$GenR, n=10, topics=3)$docs[[1]]
plotQuote(thoughtR7.2.3, main="Rep 7 topics, Topic 3")
thoughtR7.2.4 <- findThoughts(GenRChoice7.2, texts=outR$meta$GenR, n=10, topics=4)$docs[[1]]
plotQuote(thoughtR7.2.4, main="Rep 7 topics, Topic 4")
thoughtR7.2.5 <- findThoughts(GenRChoice7.2, texts=outR$meta$GenR, n=10, topics=5)$docs[[1]]
plotQuote(thoughtR7.2.5, main="Rep 7 topics, Topic 5")
thoughtR7.2.6 <- findThoughts(GenRChoice7.2, texts=outR$meta$GenR, n=10, topics=6)$docs[[1]]
plotQuote(thoughtR7.2.6, main="Rep 7 topics, Topic 6")
thoughtR7.2.7 <- findThoughts(GenRChoice7.2, texts=outR$meta$GenR, n=10, topics=7)$docs[[1]]
plotQuote(thoughtR7.2.7, main="Rep 7 topics, Topic 7")

##7.5 model##
plot(GenRChoice7.5, type="summary")
thoughtR7.5.1 <- findThoughts(GenRChoice7.5, texts=outR$meta$GenR, n=10, topics=1)$docs[[1]]
plotQuote(thoughtR7.5.1, main="Rep 7 topics, Topic 1")
thoughtR7.5.2 <- findThoughts(GenRChoice7.5, texts=outR$meta$GenR, n=10, topics=2)$docs[[1]]
plotQuote(thoughtR7.5.2, main="Rep 7 topics, Topic 2")
thoughtR7.5.3 <- findThoughts(GenRChoice7.5, texts=outR$meta$GenR, n=10, topics=3)$docs[[1]]
plotQuote(thoughtR7.5.3, main="Rep 7 topics, Topic 3")
thoughtR7.5.4 <- findThoughts(GenRChoice7.5, texts=outR$meta$GenR, n=10, topics=4)$docs[[1]]
plotQuote(thoughtR7.5.4, main="Rep 7 topics, Topic 4")
thoughtR7.5.5 <- findThoughts(GenRChoice7.5, texts=outR$meta$GenR, n=10, topics=5)$docs[[1]]
plotQuote(thoughtR7.5.5, main="Rep 7 topics, Topic 5")
thoughtR7.5.6 <- findThoughts(GenRChoice7.5, texts=outR$meta$GenR, n=10, topics=6)$docs[[1]]
plotQuote(thoughtR7.5.6, main="Rep 7 topics, Topic 6")
thoughtR7.5.7 <- findThoughts(GenRChoice7.5, texts=outR$meta$GenR, n=10, topics=7)$docs[[1]]
plotQuote(thoughtR7.5.7, main="Rep 7 topics, Topic 7")

##8.5 model##
plot(GenRChoice8.5, type="summary")
thoughtR8.5.1 <- findThoughts(GenRChoice8.5, texts=outR$meta$GenR, n=10, topics=1)$docs[[1]]
plotQuote(thoughtR8.5.1, main="Rep 8 topics, Topic 1")
thoughtR8.5.2 <- findThoughts(GenRChoice8.5, texts=outR$meta$GenR, n=10, topics=2)$docs[[1]]
plotQuote(thoughtR8.5.2, main="Rep 8 topics, Topic 2")
thoughtR8.5.3 <- findThoughts(GenRChoice8.5, texts=outR$meta$GenR, n=10, topics=3)$docs[[1]]
plotQuote(thoughtR8.5.3, main="Rep 8 topics, Topic 3")
thoughtR8.5.4 <- findThoughts(GenRChoice8.5, texts=outR$meta$GenR, n=10, topics=4)$docs[[1]]
plotQuote(thoughtR8.5.4, main="Rep 8 topics, Topic 4")
thoughtR8.5.5 <- findThoughts(GenRChoice8.5, texts=outR$meta$GenR, n=10, topics=5)$docs[[1]]
plotQuote(thoughtR8.5.5, main="Rep 8 topics, Topic 5")
thoughtR8.5.6 <- findThoughts(GenRChoice8.5, texts=outR$meta$GenR, n=10, topics=6)$docs[[1]]
plotQuote(thoughtR8.5.6, main="Rep 8 topics, Topic 6")
thoughtR8.5.7 <- findThoughts(GenRChoice8.5, texts=outR$meta$GenR, n=10, topics=7)$docs[[1]]
plotQuote(thoughtR8.5.7, main="Rep 8 topics, Topic 7")
thoughtR8.5.8 <- findThoughts(GenRChoice8.5, texts=outR$meta$GenR, n=10, topics=8)$docs[[1]]
plotQuote(thoughtR8.5.8, main="Rep 8 topics, Topic 8")

##DEMOCRATS##
##5.5 model##
plot(GenDChoice5.5, type="summary")
thoughtR5.5.1 <- findThoughts(GenDChoice5.5, texts=outD$meta$GenD, n=10, topics=1)$docs[[1]]
plotQuote(thoughtR5.5.1, main="Dem 5 topics, Topic 1")
thoughtR5.5.2 <- findThoughts(GenDChoice5.5, texts=outD$meta$GenD, n=10, topics=2)$docs[[1]]
plotQuote(thoughtR5.5.2, main="Dem 5 topics, Topic 2")
thoughtR5.5.3 <- findThoughts(GenDChoice5.5, texts=outD$meta$GenD, n=10, topics=3)$docs[[1]]
plotQuote(thoughtR5.5.3, main="Dem 5 topics, Topic 3")
thoughtR5.5.4 <- findThoughts(GenDChoice5.5, texts=outD$meta$GenD, n=10, topics=4)$docs[[1]]
plotQuote(thoughtR5.5.4, main="Dem 5 topics, Topic 4")
thoughtR5.5.5 <- findThoughts(GenDChoice5.5, texts=outD$meta$GenD, n=10, topics=5)$docs[[1]]
plotQuote(thoughtR5.5.5, main="Dem 5 topics, Topic 5")

##7.3 model##
plot(GenDChoice7.3, type="summary")
thoughtR7.3.1 <- findThoughts(GenDChoice7.3, texts=outD$meta$GenD, n=10, topics=1)$docs[[1]]
plotQuote(thoughtR7.3.1, main="Dem 7 topics, Topic 1")
thoughtR7.3.2 <- findThoughts(GenDChoice7.3, texts=outD$meta$GenD, n=10, topics=2)$docs[[1]]
plotQuote(thoughtR7.3.2, main="Dem 7 topics, Topic 2")
thoughtR7.3.3 <- findThoughts(GenDChoice7.3, texts=outD$meta$GenD, n=10, topics=3)$docs[[1]]
plotQuote(thoughtR7.3.3, main="Dem 7 topics, Topic 3")
thoughtR7.3.4 <- findThoughts(GenDChoice7.3, texts=outD$meta$GenD, n=10, topics=4)$docs[[1]]
plotQuote(thoughtR7.3.4, main="Dem 7 topics, Topic 4")
thoughtR7.3.5 <- findThoughts(GenDChoice7.3, texts=outD$meta$GenD, n=10, topics=5)$docs[[1]]
plotQuote(thoughtR7.3.5, main="Dem 7 topics, Topic 5")
thoughtR7.3.6 <- findThoughts(GenDChoice7.3, texts=outD$meta$GenD, n=10, topics=6)$docs[[1]]
plotQuote(thoughtR7.3.6, main="Dem 7 topics, Topic 6")
thoughtR7.3.7 <- findThoughts(GenDChoice7.3, texts=outD$meta$GenD, n=10, topics=7)$docs[[1]]
plotQuote(thoughtR7.3.7, main="Dem 7 topics, Topic 7")

##8.1 model##
plot(GenDChoice8.1, type="summary")
thoughtR8.1.1 <- findThoughts(GenDChoice8.1, texts=outD$meta$GenD, n=10, topics=1)$docs[[1]]
plotQuote(thoughtR8.1.1, main="Dem 8 topics, Topic 1")
thoughtR8.1.2 <- findThoughts(GenDChoice8.1, texts=outD$meta$GenD, n=10, topics=2)$docs[[1]]
plotQuote(thoughtR8.1.2, main="Dem 8 topics, Topic 2")
thoughtR8.1.3 <- findThoughts(GenDChoice8.1, texts=outD$meta$GenD, n=10, topics=3)$docs[[1]]
plotQuote(thoughtR8.1.3, main="Dem 8 topics, Topic 3")
thoughtR8.1.4 <- findThoughts(GenDChoice8.1, texts=outD$meta$GenD, n=10, topics=4)$docs[[1]]
plotQuote(thoughtR8.1.4, main="Dem 8 topics, Topic 4")
thoughtR8.1.5 <- findThoughts(GenDChoice8.1, texts=outD$meta$GenD, n=10, topics=5)$docs[[1]]
plotQuote(thoughtR8.1.5, main="Dem 8 topics, Topic 5")
thoughtR8.1.6 <- findThoughts(GenDChoice8.1, texts=outD$meta$GenD, n=10, topics=6)$docs[[1]]
plotQuote(thoughtR8.1.6, main="Dem 8 topics, Topic 6")
thoughtR8.1.7 <- findThoughts(GenDChoice8.1, texts=outD$meta$GenD, n=10, topics=7)$docs[[1]]
plotQuote(thoughtR8.1.7, main="Dem 8 topics, Topic 7")
thoughtR8.1.8 <- findThoughts(GenDChoice8.1, texts=outD$meta$GenD, n=10, topics=8)$docs[[1]]
plotQuote(thoughtR8.1.8, main="Dem 8 topics, Topic 8")


##9.5 model##
plot(GenDChoice9.5, type="summary")
thoughtR9.5.1 <- findThoughts(GenDChoice9.5, texts=outD$meta$GenD, n=10, topics=1)$docs[[1]]
plotQuote(thoughtR9.5.1, main="Dem 9 topics, Topic 1")
thoughtR9.5.2 <- findThoughts(GenDChoice9.5, texts=outD$meta$GenD, n=10, topics=2)$docs[[1]]
plotQuote(thoughtR9.5.2, main="Dem 9 topics, Topic 2")
thoughtR9.5.3 <- findThoughts(GenDChoice9.5, texts=outD$meta$GenD, n=10, topics=3)$docs[[1]]
plotQuote(thoughtR9.5.3, main="Dem 9 topics, Topic 3")
thoughtR9.5.4 <- findThoughts(GenDChoice9.5, texts=outD$meta$GenD, n=10, topics=4)$docs[[1]]
plotQuote(thoughtR9.5.4, main="Dem 9 topics, Topic 4")
thoughtR9.5.5 <- findThoughts(GenDChoice9.5, texts=outD$meta$GenD, n=10, topics=5)$docs[[1]]
plotQuote(thoughtR9.5.5, main="Dem 9 topics, Topic 5")
thoughtR9.5.6 <- findThoughts(GenDChoice9.5, texts=outD$meta$GenD, n=10, topics=6)$docs[[1]]
plotQuote(thoughtR9.5.6, main="Dem 9 topics, Topic 6")
thoughtR9.5.7 <- findThoughts(GenDChoice9.5, texts=outD$meta$GenD, n=10, topics=7)$docs[[1]]
plotQuote(thoughtR9.5.7, main="Dem 9 topics, Topic 7")
thoughtR9.5.8 <- findThoughts(GenDChoice9.5, texts=outD$meta$GenD, n=10, topics=8)$docs[[1]]
plotQuote(thoughtR9.5.8, main="Dem 9 topics, Topic 8")
thoughtR9.5.9 <- findThoughts(GenDChoice9.5, texts=outD$meta$GenD, n=10, topics=9)$docs[[1]]
plotQuote(thoughtR9.5.9, main="Dem 9 topics, Topic 9")

##FINAL TOPIC MODELS##
#TOPIC 8.1 FOR DEMS AND 8.5 FOR REPS
GenDChoice=GenDChoice8.1
GenRChoice=GenRChoice8.5

####From here, these commands build on the saved RData file with the results of the STM process####
library(TeachingDemos)
#Change the location of this file to where you would like to save the results file
setwd("G:/Shared drives/Changing Partisan Stereotypes Paper/Analysis Code/PSRM Replication files")
txtStart("Changing Partisan Stereotypes in the Trump era_log file.txt")
####Startup and libraries####
library(stm)
library(stminsights)
library(dplyr)
library(ggplot2)
library(estimatr)
library(here)
library(stringr)

options(scipen = 999)
#Change the working directories to the location where the data files are saved and where you want to save the figures
here("G:/Shared drives/Changing Partisan Stereotypes Paper/Analysis Code/PSRM Replication files")
#Read in the RData file:
load("Changing Partisan Stereotypes in the Trump Era_data file_with weights.RData")

####COMMANDS FOR THE MAIN PAPER:####
##Table 1, topic proportions and frex words##
#FREX Words
summary(GenDChoice)
#Topic proportions
summary(GenDChoice$theta)
#Exemplars:
thoughtR8.1.1 <- findThoughts(GenDChoice8.1, texts=outD$meta$GenD, n=10, topics=1)$docs[[1]]
plotQuote(thoughtR8.1.1, main="Dem 8 topics, Topic 1")
thoughtR8.1.2 <- findThoughts(GenDChoice8.1, texts=outD$meta$GenD, n=10, topics=2)$docs[[1]]
plotQuote(thoughtR8.1.2, main="Dem 8 topics, Topic 2")
thoughtR8.1.3 <- findThoughts(GenDChoice8.1, texts=outD$meta$GenD, n=10, topics=3)$docs[[1]]
plotQuote(thoughtR8.1.3, main="Dem 8 topics, Topic 3")
thoughtR8.1.4 <- findThoughts(GenDChoice8.1, texts=outD$meta$GenD, n=10, topics=4)$docs[[1]]
plotQuote(thoughtR8.1.4, main="Dem 8 topics, Topic 4")
thoughtR8.1.5 <- findThoughts(GenDChoice8.1, texts=outD$meta$GenD, n=10, topics=5)$docs[[1]]
plotQuote(thoughtR8.1.5, main="Dem 8 topics, Topic 5")
thoughtR8.1.6 <- findThoughts(GenDChoice8.1, texts=outD$meta$GenD, n=10, topics=6)$docs[[1]]
plotQuote(thoughtR8.1.6, main="Dem 8 topics, Topic 6")
thoughtR8.1.7 <- findThoughts(GenDChoice8.1, texts=outD$meta$GenD, n=10, topics=7)$docs[[1]]
plotQuote(thoughtR8.1.7, main="Dem 8 topics, Topic 7")
thoughtR8.1.8 <- findThoughts(GenDChoice8.1, texts=outD$meta$GenD, n=10, topics=8)$docs[[1]]
plotQuote(thoughtR8.1.8, main="Dem 8 topics, Topic 8")

#FN 8:
#these commands count the number of times someone skipped the open-ended text boxes
#The commands divided by 11 to scale the options to be 0, 1, 2, 3, and 4.
mergedD$nr_count=str_count(mergedD$GenD, "[NONRESPONSE]")/11
prop.table(table(mergedD$nr_count))

mergedR$nr_count=str_count(mergedR$GenR, "[NONRESPONSE]")/11
prop.table(table(mergedR$nr_count))

1-prop.table(table(mergedD$nr_count))[5]
1-prop.table(table(mergedR$nr_count))[5]

##Table 2, topic proportions and frex words##
#FREX Words:
summary(GenRChoice)
#Topic proportions:
summary(GenRChoice$theta)
#Exemplars:
thoughtR8.5.1 <- findThoughts(GenRChoice8.5, texts=outR$meta$GenR, n=10, topics=1)$docs[[1]]
plotQuote(thoughtR8.5.1, main="Rep 8 topics, Topic 1")
thoughtR8.5.2 <- findThoughts(GenRChoice8.5, texts=outR$meta$GenR, n=10, topics=2)$docs[[1]]
plotQuote(thoughtR8.5.2, main="Rep 8 topics, Topic 2")
thoughtR8.5.3 <- findThoughts(GenRChoice8.5, texts=outR$meta$GenR, n=10, topics=3)$docs[[1]]
plotQuote(thoughtR8.5.3, main="Rep 8 topics, Topic 3")
thoughtR8.5.4 <- findThoughts(GenRChoice8.5, texts=outR$meta$GenR, n=10, topics=4)$docs[[1]]
plotQuote(thoughtR8.5.4, main="Rep 8 topics, Topic 4")
thoughtR8.5.5 <- findThoughts(GenRChoice8.5, texts=outR$meta$GenR, n=10, topics=5)$docs[[1]]
plotQuote(thoughtR8.5.5, main="Rep 8 topics, Topic 5")
thoughtR8.5.6 <- findThoughts(GenRChoice8.5, texts=outR$meta$GenR, n=10, topics=6)$docs[[1]]
plotQuote(thoughtR8.5.6, main="Rep 8 topics, Topic 6")
thoughtR8.5.7 <- findThoughts(GenRChoice8.5, texts=outR$meta$GenR, n=10, topics=7)$docs[[1]]
plotQuote(thoughtR8.5.7, main="Rep 8 topics, Topic 7")
thoughtR8.5.8 <- findThoughts(GenRChoice8.5, texts=outR$meta$GenR, n=10, topics=8)$docs[[1]]
plotQuote(thoughtR8.5.8, main="Rep 8 topics, Topic 8")

##Figure 1##
#Estimate the effects in the STM package and get the objects needed for the graphs
prep_rep=estimateEffect(1:8~rep*year2021, GenRChoice, meta=outR$meta)
prep_dem=estimateEffect(1:8~dem*year2021, GenRChoice, meta=outR$meta)
rep_rep=plot.estimateEffect(prep_rep, covariate="year2021", moderator="rep", moderator.value=1, method="pointestimate")
rep_dem=plot.estimateEffect(prep_dem, covariate="year2021", moderator="dem", moderator.value=1, method="pointestimate")

prep_rep=estimateEffect(1:8~rep*year2021, GenDChoice, meta=outD$meta)
prep_dem=estimateEffect(1:8~dem*year2021, GenDChoice, meta=outD$meta)
dem_rep=plot.estimateEffect(prep_rep, covariate="year2021", moderator="rep", moderator.value=1, method="pointestimate")
dem_dem=plot.estimateEffect(prep_dem, covariate="year2021", moderator="dem", moderator.value=1, method="pointestimate")

#Now for the plotting:
party=c(rep(c("Stereotypes of Republicans", "Stereotypes of Republicans"),16),
        rep(c("Stereotypes of Democrats", "Stereotypes of Democrats"),16)
)
respondent=c(rep("Republican respondents",16), 
             rep("Democratic respondents",16),
             rep("Republican respondents",16), 
             rep("Democratic respondents",16))

topic=c(1,1,
        2,2,
        3,3,
        4,4,
        5,5,
        6,6,
        7,7,
        8,8,
        1,1,
        2,2,
        3,3,
        4,4,
        5,5,
        6,6,
        7,7,
        8,8,
        1,1,
        2,2,
        3,3,
        4,4,
        5,5,
        6,6,
        7,7,
        8,8,
        1,1,
        2,2,
        3,3,
        4,4,
        5,5,
        6,6,
        7,7,
        8,8
)

bar=c(
  rep_rep$means[[1]][[2]],
  rep_rep$means[[1]][[1]],
  rep_rep$means[[2]][[2]],
  rep_rep$means[[2]][[1]],
  rep_rep$means[[3]][[2]],
  rep_rep$means[[3]][[1]],
  rep_rep$means[[4]][[2]],
  rep_rep$means[[4]][[1]],
  rep_rep$means[[5]][[2]],
  rep_rep$means[[5]][[1]],
  rep_rep$means[[6]][[2]],
  rep_rep$means[[6]][[1]],
  rep_rep$means[[7]][[2]],
  rep_rep$means[[7]][[1]],
  rep_rep$means[[8]][[2]],
  rep_rep$means[[8]][[1]],
  rep_dem$means[[1]][[2]],
  rep_dem$means[[1]][[1]],
  rep_dem$means[[2]][[2]],
  rep_dem$means[[2]][[1]],
  rep_dem$means[[3]][[2]],
  rep_dem$means[[3]][[1]],
  rep_dem$means[[4]][[2]],
  rep_dem$means[[4]][[1]],
  rep_dem$means[[5]][[2]],
  rep_dem$means[[5]][[1]],
  rep_dem$means[[6]][[2]],
  rep_dem$means[[6]][[1]],
  rep_dem$means[[7]][[2]],
  rep_dem$means[[7]][[1]],
  rep_dem$means[[8]][[2]],
  rep_dem$means[[8]][[1]],
  dem_rep$means[[1]][[2]],
  dem_rep$means[[1]][[1]],
  dem_rep$means[[2]][[2]],
  dem_rep$means[[2]][[1]],
  dem_rep$means[[3]][[2]],
  dem_rep$means[[3]][[1]],
  dem_rep$means[[4]][[2]],
  dem_rep$means[[4]][[1]],
  dem_rep$means[[5]][[2]],
  dem_rep$means[[5]][[1]],
  dem_rep$means[[6]][[2]],
  dem_rep$means[[6]][[1]],
  dem_rep$means[[7]][[2]],
  dem_rep$means[[7]][[1]],
  dem_rep$means[[8]][[2]],
  dem_rep$means[[8]][[1]],
  dem_dem$means[[1]][[2]],
  dem_dem$means[[1]][[1]],
  dem_dem$means[[2]][[2]],
  dem_dem$means[[2]][[1]],
  dem_dem$means[[3]][[2]],
  dem_dem$means[[3]][[1]],
  dem_dem$means[[4]][[2]],
  dem_dem$means[[4]][[1]],
  dem_dem$means[[5]][[2]],
  dem_dem$means[[5]][[1]],
  dem_dem$means[[6]][[2]],
  dem_dem$means[[6]][[1]],
  dem_dem$means[[7]][[2]],
  dem_dem$means[[7]][[1]],
  dem_dem$means[[8]][[2]],
  dem_dem$means[[8]][[1]]
  
)

year=c(rep(c(2016,2021),32)
)

low=c(rep_rep$cis[[1]][3],
      rep_rep$cis[[1]][1],
      rep_rep$cis[[2]][3],
      rep_rep$cis[[2]][1],
      rep_rep$cis[[3]][3],
      rep_rep$cis[[3]][1],
      rep_rep$cis[[4]][3],
      rep_rep$cis[[4]][1],
      rep_rep$cis[[5]][3],
      rep_rep$cis[[5]][1],
      rep_rep$cis[[6]][3],
      rep_rep$cis[[6]][1],
      rep_rep$cis[[7]][3],
      rep_rep$cis[[7]][1],
      rep_rep$cis[[8]][3],
      rep_rep$cis[[8]][1],
      rep_dem$cis[[1]][3],
      rep_dem$cis[[1]][1],
      rep_dem$cis[[2]][3],
      rep_dem$cis[[2]][1],
      rep_dem$cis[[3]][3],
      rep_dem$cis[[3]][1],
      rep_dem$cis[[4]][3],
      rep_dem$cis[[4]][1],
      rep_dem$cis[[5]][3],
      rep_dem$cis[[5]][1],
      rep_dem$cis[[6]][3],
      rep_dem$cis[[6]][1],
      rep_dem$cis[[7]][3],
      rep_dem$cis[[7]][1],
      rep_dem$cis[[8]][3],
      rep_dem$cis[[8]][1],
      dem_rep$cis[[1]][3],
      dem_rep$cis[[1]][1],
      dem_rep$cis[[2]][3],
      dem_rep$cis[[2]][1],
      dem_rep$cis[[3]][3],
      dem_rep$cis[[3]][1],
      dem_rep$cis[[4]][3],
      dem_rep$cis[[4]][1],
      dem_rep$cis[[5]][3],
      dem_rep$cis[[5]][1],
      dem_rep$cis[[6]][3],
      dem_rep$cis[[6]][1],
      dem_rep$cis[[7]][3],
      dem_rep$cis[[7]][1],
      dem_rep$cis[[8]][3],
      dem_rep$cis[[8]][1],
      dem_dem$cis[[1]][3],
      dem_dem$cis[[1]][1],
      dem_dem$cis[[2]][3],
      dem_dem$cis[[2]][1],
      dem_dem$cis[[3]][3],
      dem_dem$cis[[3]][1],
      dem_dem$cis[[4]][3],
      dem_dem$cis[[4]][1],
      dem_dem$cis[[5]][3],
      dem_dem$cis[[5]][1],
      dem_dem$cis[[6]][3],
      dem_dem$cis[[6]][1],
      dem_dem$cis[[7]][3],
      dem_dem$cis[[7]][1],
      dem_dem$cis[[8]][3],
      dem_dem$cis[[8]][1]
      
)
low=as.numeric(low)
high=c(rep_rep$cis[[1]][4],
       rep_rep$cis[[1]][2],
       rep_rep$cis[[2]][4],
       rep_rep$cis[[2]][2],
       rep_rep$cis[[3]][4],
       rep_rep$cis[[3]][2],
       rep_rep$cis[[4]][4],
       rep_rep$cis[[4]][2],
       rep_rep$cis[[5]][4],
       rep_rep$cis[[5]][2],
       rep_rep$cis[[6]][4],
       rep_rep$cis[[6]][2],
       rep_rep$cis[[7]][4],
       rep_rep$cis[[7]][2],
       rep_rep$cis[[8]][4],
       rep_rep$cis[[8]][2],
       rep_dem$cis[[1]][4],
       rep_dem$cis[[1]][2],
       rep_dem$cis[[2]][4],
       rep_dem$cis[[2]][2],
       rep_dem$cis[[3]][4],
       rep_dem$cis[[3]][2],
       rep_dem$cis[[4]][4],
       rep_dem$cis[[4]][2],
       rep_dem$cis[[5]][4],
       rep_dem$cis[[5]][2],
       rep_dem$cis[[6]][4],
       rep_dem$cis[[6]][2],
       rep_dem$cis[[7]][4],
       rep_dem$cis[[7]][2],
       rep_dem$cis[[8]][4],
       rep_dem$cis[[8]][2],
       dem_rep$cis[[1]][4],
       dem_rep$cis[[1]][2],
       dem_rep$cis[[2]][4],
       dem_rep$cis[[2]][2],
       dem_rep$cis[[3]][4],
       dem_rep$cis[[3]][2],
       dem_rep$cis[[4]][4],
       dem_rep$cis[[4]][2],
       dem_rep$cis[[5]][4],
       dem_rep$cis[[5]][2],
       dem_rep$cis[[6]][4],
       dem_rep$cis[[6]][2],
       dem_rep$cis[[7]][4],
       dem_rep$cis[[7]][2],
       dem_rep$cis[[8]][4],
       dem_rep$cis[[8]][2],
       dem_dem$cis[[1]][4],
       dem_dem$cis[[1]][2],
       dem_dem$cis[[2]][4],
       dem_dem$cis[[2]][2],
       dem_dem$cis[[3]][4],
       dem_dem$cis[[3]][2],
       dem_dem$cis[[4]][4],
       dem_dem$cis[[4]][2],
       dem_dem$cis[[5]][4],
       dem_dem$cis[[5]][2],
       dem_dem$cis[[6]][4],
       dem_dem$cis[[6]][2],
       dem_dem$cis[[7]][4],
       dem_dem$cis[[7]][2],
       dem_dem$cis[[8]][4],
       dem_dem$cis[[8]][2]
)
high=as.numeric(high)

#Add these together as one data object
t=as.data.frame(cbind(party, respondent))
t$topic=topic
t$bar=bar
t$year=year
t$year=as.factor(year)
t$low=low
t$high=high

##The actual graphing now##
#Splitting this into different graphs and then combining with plot_grid
bar2=ggplot(data=t[t$party=="Stereotypes of Democrats"&t$respondent=="Democratic respondents",], aes(x=topic, y=bar, fill=year, group=year))+
  geom_bar(position=position_dodge(), stat="identity")+
  geom_errorbar(aes(ymin=low, ymax=high),width=0.2, position=position_dodge((0.9)))+
  xlab("")+
  ylab("Democratic respondents")+
  ggtitle("")+
  theme(plot.caption=element_text(hjust=0))+
  scale_fill_grey()+
  scale_x_continuous(breaks = unique(t$topic),
                     labels=c("1. Bad People", "2. Nonresponse", "3. Liberal Coalition",
                              "4. Class Interests", "5. Dishonest and Lazy", "6. Smart and Honest",
                              "7. Insincere Positive", "8. Caring and Inclusive"))+
  scale_y_continuous(limits=c(-0.06,0.7))
bar2=bar2+coord_flip()+theme_bw()+theme(legend.position = "none")

bar2.1=ggplot(data=t[t$party=="Stereotypes of Democrats"&t$respondent=="Republican respondents",], aes(x=topic, y=bar, fill=year, group=year))+
  geom_bar(position=position_dodge(), stat="identity")+
  geom_errorbar(aes(ymin=low, ymax=high),width=0.2, position=position_dodge((0.9)))+
  xlab("")+
  ylab("Republican respondents")+
  ggtitle("")+
  theme(plot.caption=element_text(hjust=0))+
  scale_fill_grey()+
  scale_x_continuous(breaks = unique(t$topic),
                     labels=c("", "", "",
                              "", "", "",
                              "", ""))+
  scale_y_continuous(limits=c(-0.06,0.7))
bar2.1=bar2.1+coord_flip()+theme_bw()+
  theme(legend.position = "none")

bar2.2=ggplot(data=t[t$party=="Stereotypes of Republicans"&t$respondent=="Democratic respondents",], aes(x=topic, y=bar, fill=year, group=year))+
  geom_bar(position=position_dodge(), stat="identity")+
  geom_errorbar(aes(ymin=low, ymax=high),width=0.2, position=position_dodge((0.9)))+
  xlab("")+
  ylab("Democratic respondents")+
  ggtitle("")+
  theme(plot.caption=element_text(hjust=0))+
  scale_fill_grey()+
  scale_x_continuous(breaks = unique(t$topic),
                     labels=c("1. Nonresponse", "2. Caring Patriots", "3. Rich White Men",
                              "4. Selfish Bigots", "5. Traditional Coalition", "6. Class Interests",
                              "7. Religious \nFundamentalists", "8. Insincere Positive"))+
  scale_y_continuous(limits=c(-0.06,0.7))
bar2.2=bar2.2+coord_flip()+theme_bw()+
  theme(legend.position = "bottom",
        legend.key.size=unit(0.3, "cm"),
        legend.text=element_text(size=8),
        legend.margin=margin(0))+ theme(legend.title=element_blank())

bar2.3=ggplot(data=t[t$party=="Stereotypes of Republicans"&t$respondent=="Republican respondents",], aes(x=topic, y=bar, fill=year, group=year))+
  geom_bar(position=position_dodge(), stat="identity")+
  geom_errorbar(aes(ymin=low, ymax=high),width=0.2, position=position_dodge((0.9)))+
  xlab("")+
  ylab("Republican respondents")+
  ggtitle("")+
  labs(caption="Figure presents topic proportions along with 95 percent confidence intervals")+
  theme(plot.caption=element_text(hjust=0))+
  scale_fill_grey()+
  scale_x_continuous(breaks = unique(t$topic),
                     labels=c("", "", "",
                              "", "", "",
                              "", ""))+
  scale_y_continuous(limits=c(-0.06,0.7))
bar2.3=bar2.3+coord_flip()+theme_bw()+
  theme(legend.position = "none")

library(cowplot)
jpeg("Figure 1.jpeg", width=10, height=8, units="in", res=600)
plot_grid(bar2, bar2.1, bar2.2, bar2.3, labels=c("Stereotypes of Democrats", "", "Stereotypes of Republicans", ""),
          rel_widths = c(1.1,0.9, 1.5, 1), rel_heights = c(1,1,1,0.85))
dev.off()

##FN 9, the amount of change
#Democrats stereotypes of Democrats
topic=c(1,2,3,4,5,6,7,8)
changedd=c(abs(t[50,4]-t[49,4]),
           abs(t[52,4]-t[51,4]),
           abs(t[54,4]-t[53,4]),
           abs(t[56,4]-t[55,4]),
           abs(t[58,4]-t[57,4]),
           abs(t[60,4]-t[59,4]),
           abs(t[62,4]-t[61,4]),
           abs(t[64,4]-t[63,4]))


#Republicans' stereotypes of Democrats
changerd=c(abs(t[34,4]-t[33,4]),
           abs(t[36,4]-t[35,4]),
           abs(t[38,4]-t[37,4]),
           abs(t[40,4]-t[39,4]),
           abs(t[42,4]-t[41,4]),
           abs(t[44,4]-t[43,4]),
           abs(t[46,4]-t[45,4]),
           abs(t[48,4]-t[47,4]))

#Democrats stereotypes of Republicans
changedr=c(abs(t[18,4]-t[17,4]),
           abs(t[20,4]-t[19,4]),
           abs(t[22,4]-t[21,4]),
           abs(t[24,4]-t[23,4]),
           abs(t[26,4]-t[25,4]),
           abs(t[28,4]-t[27,4]),
           abs(t[30,4]-t[29,4]),
           abs(t[32,4]-t[31,4]))


#Republicans' stereotypes of Republicans
changerr=c(abs(t[2,4]-t[1,4]),
           abs(t[4,4]-t[3,4]),
           abs(t[6,4]-t[5,4]),
           abs(t[8,4]-t[7,4]),
           abs(t[10,4]-t[9,4]),
           abs(t[12,4]-t[11,4]),
           abs(t[14,4]-t[13,4]),
           abs(t[16,4]-t[15,4]))

c=as.data.frame(cbind(topic, changedd, changerd, changedr, changerr))
#Average change across topics
mean(c$changedd)
mean(c$changedr)
mean(c$changerd)
mean(c$changerr)

##Proportion of insincere respondents:
#This is based off of insincere answers to the secretary of state political knowledge
#question. We need to import those scores and merge them with the other data objects,
#as they were not included in the original data file.
#In these files, the research team coded the secretary of state answers for terms
#that indicate gibberish and non-sequitur answers. Examples and more details can be
#found in Appendix G.

#Insincere respondents correspond with the scores of 2 and 3 on the new variable

#First, import the data with the coded scores:
library(readr)
X2016_sos <- read_csv("G:/Shared drives/Changing Partisan Stereotypes Paper/Analysis Code/PSRM Replication files/data_2016_long_with_SoS_coded.csv")
X2021_sos <- read_csv("G:/Shared drives/Changing Partisan Stereotypes Paper/Analysis Code/PSRM Replication files/2021_SoS.csv")
#Make the year variable for the 2021 data
X2021_sos$year2021=1
X2016_sos$year2021=0
#Harmonize the data in these datasets
X2021_sos=rename(X2021_sos, PK_SoS_scored=know4_scored)

#Recode income
X2016_sos$inc2=recode(X2016_sos$inc,"Less than $15K"=1, "$15K to $25K"=2, "$25K to $50K"=3,
                      "$50K to $75K"=4, "$75K to $100K"=5, "$100K to $150K"=6,
                      "$150K to $200K"=7, "$200K to $250K"=8, "$250K to $500K"=9)
X2016_sos$inc=NULL
X2016_sos=rename(X2016_sos, inc=inc2)
#Keep only the relevant items
X2016_sos=subset(X2016_sos, select=c("GenR_1","GenR_2","GenR_3","GenR_4","GenD_1","GenD_2","GenD_3","GenD_4",
                                     "GenR", "GenD",
                                     "interest", "age", "dem", "rep", "ind","year2021",
                                     "inc","education2", "white", "hisp2", "gender", "PK_SoS_scored", "pk_scale"))


X2021_sos=subset(X2021_sos, select=c("GenR_1","GenR_2","GenR_3","GenR_4","GenD_1","GenD_2","GenD_3","GenD_4",
                                     "GenR", "GenD",
                                     "interest", "age", "dem", "rep", "ind","year2021",
                                     "inc","education2", "white", "hisp2", "gender", "PK_SoS_scored", "pk_scale"))
#Combined these together
SoS=rbind(X2021_sos, X2016_sos)

#Some recoding corrections on various variables
SoS$hisp2[SoS$hisp2=="Hispanic"]=1
SoS$hisp2[SoS$hisp2=="Not Hispanic"]=0
SoS$hisp2=as.numeric(SoS$hisp2)

SoS$gender[SoS$gender=="Female"]=1
SoS$gender[SoS$gender=="Male"]=0
SoS$gender=as.numeric(SoS$gender)

SoS$GenD_1=tolower(SoS$GenD_1)
SoS$GenD_2=tolower(SoS$GenD_2)
SoS$GenD_3=tolower(SoS$GenD_3)
SoS$GenD_4=tolower(SoS$GenD_4)

SoS$GenR_1=tolower(SoS$GenR_1)
SoS$GenR_2=tolower(SoS$GenR_2)
SoS$GenR_3=tolower(SoS$GenR_3)
SoS$GenR_4=tolower(SoS$GenR_4)
SoS$GenD_1[SoS$GenD_1=="NA"]=""
SoS$GenD_2[SoS$GenD_2=="NA"]=""
SoS$GenD_3[SoS$GenD_3=="NA"]=""
SoS$GenD_4[SoS$GenD_4=="NA"]=""
SoS$GenR_1[SoS$GenR_1=="NA"]=""
SoS$GenR_2[SoS$GenR_2=="NA"]=""
SoS$GenR_3[SoS$GenR_3=="NA"]=""
SoS$GenR_4[SoS$GenR_4=="NA"]=""


#From here, we want to merge this into the mergedD and mergedR data objects
#Must rename some of the variables first
mergedD2=rename(mergedD, GenR_1=GenR_1.x,GenR_2=GenR_2.x,GenR_3=GenR_3.x,GenR_4=GenR_4.x)
mergedR2=rename(mergedR, GenD_1=GenD_1.x,GenD_2=GenD_2.x,GenD_3=GenD_3.x,GenD_4=GenD_4.x)

mergedD2=  mergedD2 %>%mutate(
  is_duplicate=duplicated(select(., GenD, dem, rep, ind, inc, 
                                 education2, white, hisp2, gender, pk_scale, age, interest, year2021))
)

mergedR2=  mergedR2 %>%mutate(
  is_duplicate=duplicated(select(., GenR, dem, rep, ind, inc, 
                                 education2, white, hisp2, gender, pk_scale, age, interest, year2021))
)

mergedD3=left_join(mergedD2[mergedD2$is_duplicate==F,], 
                   SoS, by=c("GenD",
                             "GenR",
                             "dem", "rep", "ind", "inc", "education2",
                             "white", "hisp2", "gender", "pk_scale",
                             "age", "year2021"))

mergedR3=left_join(mergedR2[mergedR2$is_duplicate==F,], 
                   SoS, by=c("GenD", 
                             "GenR",
                             "dem", "rep", "ind", "inc", "education2",
                             "white", "hisp2", "gender", "pk_scale",
                             "age", "year2021"))

#Proportion giving insincere responses (these are marked as 2 and 3):
#We do this separately for the Republican and Democrat stereotype data objects
prop.table(table(mergedD3$PK_SoS_scored))
prop.table(table(mergedD3$PK_SoS_scored))[3]+prop.table(table(mergedD3$PK_SoS_scored))[4]

prop.table(table(mergedR3$PK_SoS_scored))
prop.table(table(mergedR3$PK_SoS_scored))[3]+prop.table(table(mergedR3$PK_SoS_scored))[4]

#2016
prop.table(table(mergedD3$PK_SoS_scored[mergedD3$year2021==0]))[3]+prop.table(table(mergedD3$PK_SoS_scored[mergedD3$year2021==0]))[4]
prop.table(table(mergedR3$PK_SoS_scored[mergedD3$year2021==0]))[3]+prop.table(table(mergedR3$PK_SoS_scored[mergedD3$year2021==0]))[4]

#2021
prop.table(table(mergedD3$PK_SoS_scored[mergedD3$year2021==1]))[3]+prop.table(table(mergedD3$PK_SoS_scored[mergedD3$year2021==1]))[4]
prop.table(table(mergedR3$PK_SoS_scored[mergedD3$year2021==1]))[3]+prop.table(table(mergedR3$PK_SoS_scored[mergedD3$year2021==1]))[4]

####COMMANDS FOR THE APPENDIX:####
####Appendix A:####
#WHERE IS THE HANDCODING STUFF?

####Appendix B:####
#Table B1 on demographics#
prop.table(table(data$gender, data$year2021), 2)
prop.table(table(data$dem, data$year2021), 2)
prop.table(table(data$rep, data$year2021), 2)
prop.table(table(data$white, data$year2021), 2)
prop.table(table(data$hisp2, data$year2021), 2)
t.test(data$age~data$year2021)
t.test(data$inc~data$year2021)
t.test(data$education2~data$year2021)
t.test(data$interest~data$year2021)
t.test(data$pk_scale~data$year2021)

#Estimates using weights#
#The objective of these commands is to create a set of weights that we can then
#apply to our stereotypes data to try to make the samples more comparable
#to each other.

#This code follows the example/tutorial found here:
# https://zacharylhertz.github.io/posts/2022/05/weighting-surveys

#Here are the variables we are going to use to construct the weights:
#   Age
#   Income
#   Education
#   White
#   Hispanic
#   Gender

#Here's the relevant packages
library(data.table)
library(survey)
library(tidyverse)
library(anesrake)
#non-scientific notation
options(scipen = 999)

#Selectively read in the census data variables that we want:
#NOTE THAT THESE FILES MUST BE DOWNLOADED FROM THE CENSUS; WE USE THE 5 YEAR CENSUS
#ESTIMATES FROM 2017-2021. THOSE FILES CAN BE ACCESSED HERE: https://www2.census.gov/programs-surveys/acs/data/pums/
#We also provide these files from the Census along with our other data files.
#Change thelocation of the census files to match their location on your device.

a=fread("psam_pusa.csv", 
        select=c("AGEP", "PINCP", "SCHL", "RAC1P", "HISP", "SEX", "PWGTP"))

b=fread("psam_pusb.csv", 
        select=c("AGEP", "PINCP", "SCHL", "RAC1P", "HISP", "SEX", "PWGTP"))

c=fread("psam_pusc.csv", 
        select=c("AGEP", "PINCP", "SCHL", "RAC1P", "HISP", "SEX", "PWGTP"))

d=fread("psam_pusd.csv", 
        select=c("AGEP", "PINCP", "SCHL", "RAC1P", "HISP", "SEX", "PWGTP"))

#combine the census files:
acs=rbind(a,b,c,d)
#Remove old objects
rm(a,b,c,d)
#keep only adults
acs=subset(acs, acs$AGEP>17)

#Recoding the variables to match our data; the coding must match exactly for the
#weighting commands to work.

#Here are the distributions from our data
#We're going to do this separately for the mergedD and mergedR data objects. That
#contain the respondents demographics and the topics for D and R stereotypes

table(mergedD$white[mergedD$year2021==1])
table(mergedD$white[mergedD$year2021==0])
table(mergedR$white[mergedR$year2021==1])
table(mergedR$white[mergedR$year2021==0])

table(acs$RAC1P)

acs$white2[acs$RAC1P==1]=2
acs$white2[acs$RAC1P>1]=1
mergedD$white2=mergedD$white+1
mergedR$white2=mergedR$white+1

table(mergedD$hisp2[mergedD$year2021==1])
table(mergedD$hisp2[mergedD$year2021==0])
table(acs$HISP)
table(mergedR$hisp2[mergedR$year2021==1])
table(mergedR$hisp2[mergedR$year2021==0])


acs$hisp3[acs$HISP>1]=2
acs$hisp3[acs$HISP==1]=1
mergedD$hisp3=mergedD$hisp2+1
mergedR$hisp3=mergedR$hisp2+1

table(mergedD$age)
table(mergedR$age)
table(acs$AGEP)

mergedD$age5[mergedD$age<30]=1 #18-29
mergedD$age5[mergedD$age>29 & mergedD$age<45]=2 #30-44
mergedD$age5[mergedD$age>44 & mergedD$age<55]=3 #45-54
mergedD$age5[mergedD$age>54 & mergedD$age<65]=4 #55-64
mergedD$age5[mergedD$age>64]=5 #65+
mergedD$age5=as.numeric(mergedD$age5)
mergedR$age5[mergedR$age<30]=1 #18-29
mergedR$age5[mergedR$age>29 & mergedR$age<45]=2 #30-44
mergedR$age5[mergedR$age>44 & mergedR$age<55]=3 #45-54
mergedR$age5[mergedR$age>54 & mergedR$age<65]=4 #55-64
mergedR$age5[mergedR$age>64]=5 #65+
mergedR$age5=as.numeric(mergedR$age5)


acs$age5[acs$AGEP<30]=1 #18-29
acs$age5[acs$AGEP>29 & acs$AGEP<45]=2 #30-44
acs$age5[acs$AGEP>44 & acs$AGEP<55]=3 #45-54
acs$age5[acs$AGEP>54 & acs$AGEP<65]=4 #55-64
acs$age5[acs$AGEP>64]=5 #65+

table(mergedD$gender) #1 is female
table(mergedR$gender) #1 is female
table(acs$SEX)

acs$gender2[acs$SEX==2]=2
acs$gender2[acs$SEX==1]=1
mergedD$gender2=mergedD$gender+1
mergedR$gender2=mergedR$gender+1

table(mergedD$education2)
table(acs$SCHL)

acs$education2[acs$SCHL<16]=1
acs$education2[acs$SCHL==16|acs$SCHL==17]=2
acs$education2[acs$SCHL==18|acs$SCHL==19]=3
acs$education2[acs$SCHL==20]=4
acs$education2[acs$SCHL==21]=5
acs$education2[acs$SCHL==22]=6
acs$education2[acs$SCHL>22]=7

table(mergedD$inc)
table(acs$PINCP)

acs$inc[acs$PINCP<15000]=1
acs$inc[acs$PINCP>=15000&acs$PINCP<25000]=2
acs$inc[acs$PINCP>=25000&acs$PINCP<50000]=3
acs$inc[acs$PINCP>=50000&acs$PINCP<75000]=4
acs$inc[acs$PINCP>=75000&acs$PINCP<100000]=5
acs$inc[acs$PINCP>=100000&acs$PINCP<150000]=6
acs$inc[acs$PINCP>=150000&acs$PINCP<200000]=7
acs$inc[acs$PINCP>=200000&acs$PINCP<250000]=8
acs$inc[acs$PINCP>=250000]=9

#Creating the weighting benchmarks
svy.acs <- svydesign(ids=~1, data=acs, weights=acs$PWGTP) 
rm(acs) # remove the ACS to save memory

#Create the targets:
gender2 <- svytable(~gender2, design=svy.acs) %>%
  prop.table() %>%
  round(digits=3) %>%
  as.numeric() # 

hisp3 <- svytable(~hisp3, design=svy.acs) %>%
  prop.table() %>%
  round(digits=3) %>%
  as.numeric() # 

white2 <- svytable(~white2, design=svy.acs) %>%
  prop.table() %>%
  round(digits=3) %>%
  as.numeric() # 

age5 <- svytable(~age5, design=svy.acs) %>%
  prop.table() %>%
  round(digits=3) %>%
  as.numeric() # 

education2 <- svytable(~education2, design=svy.acs) %>%
  prop.table() %>%
  round(digits=3) %>%
  as.numeric() # 

inc <- svytable(~inc, design=svy.acs) %>%
  prop.table() %>%
  round(digits=3) %>%
  as.numeric() # 

#Combine the targets into a single list
targets <- list(gender2, hisp3, white2, age5, education2, inc)
# remember, these names will have to match
names(targets) <- c("gender2", "hisp3", "white2", "age5", "education2", "inc")

#Bring in our data
#We want to weight the two years separately so that we can have weighted samples 
#for each year that address any demographic differences in the sample

#We're going to do this separately for the mergedD and mergedR data objects. That
#contain the respondents demographics and the topics for D and R stereotypes

#MergedR object
#2016 weights
w_2016=anesrake(targets, mergedR[mergedR$year2021==0,],
                caseid=mergedR$ID, type="nolim")
mergedR.2016=mergedR[mergedR$year2021==0,]
#2021 weights
w_2021=anesrake(targets, mergedR[mergedR$year2021==1,],
                caseid=mergedR$ID, type="nolim")

mergedR$weight[mergedR$year2021==1]=unlist(w_2021[1])
mergedR$weight[mergedR$year2021==0]=unlist(w_2016[1])

mergedR.weighted=svydesign(ids=~1, data=mergedR, weights=mergedR$weight)

#MergedD object
#2016 weights
wd_2016=anesrake(targets, mergedD[mergedD$year2021==0,],
                 caseid=mergedD$ID, type="nolim")
mergedD.2016=mergedD[mergedD$year2021==0,]
#2021 weights
wd_2021=anesrake(targets, mergedD[mergedD$year2021==1,],
                 caseid=mergedD$ID, type="nolim")

mergedD$weight[mergedD$year2021==1]=unlist(wd_2021[1])
mergedD$weight[mergedD$year2021==0]=unlist(wd_2016[1])

mergedD.weighted=svydesign(ids=~1, data=mergedD, weights=mergedD$weight)

#Just for 2016
mergedD.weighted.2016=svydesign(ids=~1, data=mergedD[mergedD$year2021==0,], weights=mergedD$weight[mergedD$year2021==0])

#Check the distribution of the variables with and without the weights
prop.table(table(mergedD$education2[mergedD$year2021==0]))
prop.table(svytable(~education2, design=mergedD.weighted.2016))
targets$education2

prop.table(table(mergedD$inc[mergedD$year2021==0]))
prop.table(svytable(~inc, design=mergedD.weighted.2016))
targets$inc

prop.table(table(mergedD$gender2[mergedD$year2021==0]))
prop.table(svytable(~gender2, design=mergedD.weighted.2016))
targets$gender2

prop.table(table(mergedD$hisp3[mergedD$year2021==0]))
prop.table(svytable(~hisp3, design=mergedD.weighted.2016))
targets$hisp3

prop.table(table(mergedD$white2[mergedD$year2021==0]))
prop.table(svytable(~white2, design=mergedD.weighted.2016))
targets$white2

prop.table(table(mergedD$age5[mergedD$year2021==0]))
prop.table(svytable(~age5, design=mergedD.weighted.2016))
targets$age5

#Just for 2021
mergedD.weighted.2021=svydesign(ids=~1, data=mergedD[mergedD$year2021==1,], weights=mergedD$weight[mergedD$year2021==1])

#Check the distribution of the variables with and without the weights
prop.table(table(mergedD$education2[mergedD$year2021==1]))
prop.table(svytable(~education2, design=mergedD.weighted.2021))
targets$education2

prop.table(table(mergedD$inc[mergedD$year2021==1]))
prop.table(svytable(~inc, design=mergedD.weighted.2021))
targets$inc

prop.table(table(mergedD$gender2[mergedD$year2021==1]))
prop.table(svytable(~gender2, design=mergedD.weighted.2021))
targets$gender2

prop.table(table(mergedD$hisp3[mergedD$year2021==1]))
prop.table(svytable(~hisp3, design=mergedD.weighted.2021))
targets$hisp3

prop.table(table(mergedD$white2[mergedD$year2021==1]))
prop.table(svytable(~white2, design=mergedD.weighted.2021))
targets$white2

prop.table(table(mergedD$age5[mergedD$year2021==1]))
prop.table(svytable(~age5, design=mergedD.weighted.2021))
targets$age5

##MergedR

#Just for 2016
mergedR.weighted.2016=svydesign(ids=~1, data=mergedR[mergedR$year2021==0,], weights=mergedR$weight[mergedR$year2021==0])

#Check the distribution of the variables with and without the weights
prop.table(table(mergedR$education2[mergedR$year2021==0]))
prop.table(svytable(~education2, design=mergedR.weighted.2016))
targets$education2

prop.table(table(mergedR$inc[mergedR$year2021==0]))
prop.table(svytable(~inc, design=mergedR.weighted.2016))
targets$inc

prop.table(table(mergedR$gender2[mergedR$year2021==0]))
prop.table(svytable(~gender2, design=mergedR.weighted.2016))
targets$gender2

prop.table(table(mergedR$hisp3[mergedR$year2021==0]))
prop.table(svytable(~hisp3, design=mergedR.weighted.2016))
targets$hisp3

prop.table(table(mergedR$white2[mergedR$year2021==0]))
prop.table(svytable(~white2, design=mergedR.weighted.2016))
targets$white2

prop.table(table(mergedR$age5[mergedR$year2021==0]))
prop.table(svytable(~age5, design=mergedR.weighted.2016))
targets$age5

#Just for 2021
mergedR.weighted.2021=svydesign(ids=~1, data=mergedR[mergedR$year2021==1,], weights=mergedR$weight[mergedR$year2021==1])

#Check the distribution of the variables with and without the weights
prop.table(table(mergedR$education2[mergedR$year2021==1]))
prop.table(svytable(~education2, design=mergedR.weighted.2021))
targets$education2

prop.table(table(mergedR$inc[mergedR$year2021==1]))
prop.table(svytable(~inc, design=mergedR.weighted.2021))
targets$inc

prop.table(table(mergedR$gender2[mergedR$year2021==1]))
prop.table(svytable(~gender2, design=mergedR.weighted.2021))
targets$gender2

prop.table(table(mergedR$hisp3[mergedR$year2021==1]))
prop.table(svytable(~hisp3, design=mergedR.weighted.2021))
targets$hisp3

prop.table(table(mergedR$white2[mergedR$year2021==1]))
prop.table(svytable(~white2, design=mergedR.weighted.2021))
targets$white2

prop.table(table(mergedR$age5[mergedR$year2021==1]))
prop.table(svytable(~age5, design=mergedR.weighted.2021))
targets$age5

#Figure B.1 - weighted mean estimates:
year=c(2016, 2016, 2016, 2016, 2021, 2021,2021, 2021)
party_temp=c("Republican", "Democrat", "Republican", "Democrat","Republican", "Democrat", "Republican", "Democrat")
weights=c("Weighted","Weighted", "Unweighted", "Unweighted","Weighted","Weighted", "Unweighted", "Unweighted")
means.graph=data.frame(cbind(year, party_temp, weights))

library(plotrix)
#D topic 1
means.graph$D1=NA
means.graph$D1.se=NA
means.graph$D1[1]=svyby(~Topic1, ~dem+rep, mergedD.weighted.2016, svymean)$Topic1[3]
means.graph$D1.se[1]=svyby(~Topic1, ~dem+rep, mergedD.weighted.2016, svymean)$se[3]
means.graph$D1[2]=svyby(~Topic1, ~dem+rep, mergedD.weighted.2016, svymean)$Topic1[2]
means.graph$D1.se[2]=svyby(~Topic1, ~dem+rep, mergedD.weighted.2016, svymean)$se[2]

means.graph$D1[3]=mean(mergedD$Topic1[mergedD$rep==1&mergedD$year2021==0], na.rm=T)
means.graph$D1.se[3]=std.error(mergedD$Topic1[mergedD$rep==1&mergedD$year2021==0])
means.graph$D1[4]=mean(mergedD$Topic1[mergedD$dem==1&mergedD$year2021==0], na.rm=T)
means.graph$D1.se[4]=std.error(mergedD$Topic1[mergedD$dem==1&mergedD$year2021==0])

means.graph$D1[5]=svyby(~Topic1, ~dem+rep, mergedD.weighted.2021, svymean)$Topic1[3]
means.graph$D1.se[5]=svyby(~Topic1, ~dem+rep, mergedD.weighted.2021, svymean)$se[3]
means.graph$D1[6]=svyby(~Topic1, ~dem+rep, mergedD.weighted.2021, svymean)$Topic1[2]
means.graph$D1.se[6]=svyby(~Topic1, ~dem+rep, mergedD.weighted.2021, svymean)$se[2]

means.graph$D1[7]=mean(mergedD$Topic1[mergedD$rep==1&mergedD$year2021==1], na.rm=T)
means.graph$D1.se[7]=std.error(mergedD$Topic1[mergedD$rep==1&mergedD$year2021==1])
means.graph$D1[8]=mean(mergedD$Topic1[mergedD$dem==1&mergedD$year2021==1], na.rm=T)
means.graph$D1.se[8]=std.error(mergedD$Topic1[mergedD$dem==1&mergedD$year2021==1])

#DTopic2
means.graph$D2=NA
means.graph$D2.se=NA
means.graph$D2[1]=svyby(~Topic2, ~dem+rep, mergedD.weighted.2016, svymean)$Topic2[3]
means.graph$D2.se[1]=svyby(~Topic2, ~dem+rep, mergedD.weighted.2016, svymean)$se[3]
means.graph$D2[2]=svyby(~Topic2, ~dem+rep, mergedD.weighted.2016, svymean)$Topic2[2]
means.graph$D2.se[2]=svyby(~Topic2, ~dem+rep, mergedD.weighted.2016, svymean)$se[2]

means.graph$D2[3]=mean(mergedD$Topic2[mergedD$rep==1&mergedD$year2021==0], na.rm=T)
means.graph$D2.se[3]=std.error(mergedD$Topic2[mergedD$rep==1&mergedD$year2021==0])
means.graph$D2[4]=mean(mergedD$Topic2[mergedD$dem==1&mergedD$year2021==0], na.rm=T)
means.graph$D2.se[4]=std.error(mergedD$Topic2[mergedD$dem==1&mergedD$year2021==0])

means.graph$D2[5]=svyby(~Topic2, ~dem+rep, mergedD.weighted.2021, svymean)$Topic2[3]
means.graph$D2.se[5]=svyby(~Topic2, ~dem+rep, mergedD.weighted.2021, svymean)$se[3]
means.graph$D2[6]=svyby(~Topic2, ~dem+rep, mergedD.weighted.2021, svymean)$Topic2[2]
means.graph$D2.se[6]=svyby(~Topic2, ~dem+rep, mergedD.weighted.2021, svymean)$se[2]

means.graph$D2[7]=mean(mergedD$Topic2[mergedD$rep==1&mergedD$year2021==1], na.rm=T)
means.graph$D2.se[7]=std.error(mergedD$Topic2[mergedD$rep==1&mergedD$year2021==1])
means.graph$D2[8]=mean(mergedD$Topic2[mergedD$dem==1&mergedD$year2021==1], na.rm=T)
means.graph$D2.se[8]=std.error(mergedD$Topic2[mergedD$dem==1&mergedD$year2021==1])

#DTopic3
means.graph$D3=NA
means.graph$D3.se=NA
means.graph$D3[1]=svyby(~Topic3, ~dem+rep, mergedD.weighted.2016, svymean)$Topic3[3]
means.graph$D3.se[1]=svyby(~Topic3, ~dem+rep, mergedD.weighted.2016, svymean)$se[3]
means.graph$D3[2]=svyby(~Topic3, ~dem+rep, mergedD.weighted.2016, svymean)$Topic3[2]
means.graph$D3.se[2]=svyby(~Topic3, ~dem+rep, mergedD.weighted.2016, svymean)$se[2]

means.graph$D3[3]=mean(mergedD$Topic3[mergedD$rep==1&mergedD$year2021==0], na.rm=T)
means.graph$D3.se[3]=std.error(mergedD$Topic3[mergedD$rep==1&mergedD$year2021==0])
means.graph$D3[4]=mean(mergedD$Topic3[mergedD$dem==1&mergedD$year2021==0], na.rm=T)
means.graph$D3.se[4]=std.error(mergedD$Topic3[mergedD$dem==1&mergedD$year2021==0])

means.graph$D3[5]=svyby(~Topic3, ~dem+rep, mergedD.weighted.2021, svymean)$Topic3[3]
means.graph$D3.se[5]=svyby(~Topic3, ~dem+rep, mergedD.weighted.2021, svymean)$se[3]
means.graph$D3[6]=svyby(~Topic3, ~dem+rep, mergedD.weighted.2021, svymean)$Topic3[2]
means.graph$D3.se[6]=svyby(~Topic3, ~dem+rep, mergedD.weighted.2021, svymean)$se[2]

means.graph$D3[7]=mean(mergedD$Topic3[mergedD$rep==1&mergedD$year2021==1], na.rm=T)
means.graph$D3.se[7]=std.error(mergedD$Topic3[mergedD$rep==1&mergedD$year2021==1])
means.graph$D3[8]=mean(mergedD$Topic3[mergedD$dem==1&mergedD$year2021==1], na.rm=T)
means.graph$D3.se[8]=std.error(mergedD$Topic3[mergedD$dem==1&mergedD$year2021==1])

#DTopic4
means.graph$D4=NA
means.graph$D4.se=NA
means.graph$D4[1]=svyby(~Topic4, ~dem+rep, mergedD.weighted.2016, svymean)$Topic4[3]
means.graph$D4.se[1]=svyby(~Topic4, ~dem+rep, mergedD.weighted.2016, svymean)$se[3]
means.graph$D4[2]=svyby(~Topic4, ~dem+rep, mergedD.weighted.2016, svymean)$Topic4[2]
means.graph$D4.se[2]=svyby(~Topic4, ~dem+rep, mergedD.weighted.2016, svymean)$se[2]

means.graph$D4[3]=mean(mergedD$Topic4[mergedD$rep==1&mergedD$year2021==0], na.rm=T)
means.graph$D4.se[3]=std.error(mergedD$Topic4[mergedD$rep==1&mergedD$year2021==0])
means.graph$D4[4]=mean(mergedD$Topic4[mergedD$dem==1&mergedD$year2021==0], na.rm=T)
means.graph$D4.se[4]=std.error(mergedD$Topic4[mergedD$dem==1&mergedD$year2021==0])

means.graph$D4[5]=svyby(~Topic4, ~dem+rep, mergedD.weighted.2021, svymean)$Topic4[3]
means.graph$D4.se[5]=svyby(~Topic4, ~dem+rep, mergedD.weighted.2021, svymean)$se[3]
means.graph$D4[6]=svyby(~Topic4, ~dem+rep, mergedD.weighted.2021, svymean)$Topic4[2]
means.graph$D4.se[6]=svyby(~Topic4, ~dem+rep, mergedD.weighted.2021, svymean)$se[2]

means.graph$D4[7]=mean(mergedD$Topic4[mergedD$rep==1&mergedD$year2021==1], na.rm=T)
means.graph$D4.se[7]=std.error(mergedD$Topic4[mergedD$rep==1&mergedD$year2021==1])
means.graph$D4[8]=mean(mergedD$Topic4[mergedD$dem==1&mergedD$year2021==1], na.rm=T)
means.graph$D4.se[8]=std.error(mergedD$Topic4[mergedD$dem==1&mergedD$year2021==1])

#DTopic5
means.graph$D5=NA
means.graph$D5.se=NA
means.graph$D5[1]=svyby(~Topic5, ~dem+rep, mergedD.weighted.2016, svymean)$Topic5[3]
means.graph$D5.se[1]=svyby(~Topic5, ~dem+rep, mergedD.weighted.2016, svymean)$se[3]
means.graph$D5[2]=svyby(~Topic5, ~dem+rep, mergedD.weighted.2016, svymean)$Topic5[2]
means.graph$D5.se[2]=svyby(~Topic5, ~dem+rep, mergedD.weighted.2016, svymean)$se[2]

means.graph$D5[3]=mean(mergedD$Topic5[mergedD$rep==1&mergedD$year2021==0], na.rm=T)
means.graph$D5.se[3]=std.error(mergedD$Topic5[mergedD$rep==1&mergedD$year2021==0])
means.graph$D5[4]=mean(mergedD$Topic5[mergedD$dem==1&mergedD$year2021==0], na.rm=T)
means.graph$D5.se[4]=std.error(mergedD$Topic5[mergedD$dem==1&mergedD$year2021==0])

means.graph$D5[5]=svyby(~Topic5, ~dem+rep, mergedD.weighted.2021, svymean)$Topic5[3]
means.graph$D5.se[5]=svyby(~Topic5, ~dem+rep, mergedD.weighted.2021, svymean)$se[3]
means.graph$D5[6]=svyby(~Topic5, ~dem+rep, mergedD.weighted.2021, svymean)$Topic5[2]
means.graph$D5.se[6]=svyby(~Topic5, ~dem+rep, mergedD.weighted.2021, svymean)$se[2]

means.graph$D5[7]=mean(mergedD$Topic5[mergedD$rep==1&mergedD$year2021==1], na.rm=T)
means.graph$D5.se[7]=std.error(mergedD$Topic5[mergedD$rep==1&mergedD$year2021==1])
means.graph$D5[8]=mean(mergedD$Topic5[mergedD$dem==1&mergedD$year2021==1], na.rm=T)
means.graph$D5.se[8]=std.error(mergedD$Topic5[mergedD$dem==1&mergedD$year2021==1])

#DTopic6
means.graph$D6=NA
means.graph$D6.se=NA
means.graph$D6[1]=svyby(~Topic6, ~dem+rep, mergedD.weighted.2016, svymean)$Topic6[3]
means.graph$D6.se[1]=svyby(~Topic6, ~dem+rep, mergedD.weighted.2016, svymean)$se[3]
means.graph$D6[2]=svyby(~Topic6, ~dem+rep, mergedD.weighted.2016, svymean)$Topic6[2]
means.graph$D6.se[2]=svyby(~Topic6, ~dem+rep, mergedD.weighted.2016, svymean)$se[2]

means.graph$D6[3]=mean(mergedD$Topic6[mergedD$rep==1&mergedD$year2021==0], na.rm=T)
means.graph$D6.se[3]=std.error(mergedD$Topic6[mergedD$rep==1&mergedD$year2021==0])
means.graph$D6[4]=mean(mergedD$Topic6[mergedD$dem==1&mergedD$year2021==0], na.rm=T)
means.graph$D6.se[4]=std.error(mergedD$Topic6[mergedD$dem==1&mergedD$year2021==0])

means.graph$D6[5]=svyby(~Topic6, ~dem+rep, mergedD.weighted.2021, svymean)$Topic6[3]
means.graph$D6.se[5]=svyby(~Topic6, ~dem+rep, mergedD.weighted.2021, svymean)$se[3]
means.graph$D6[6]=svyby(~Topic6, ~dem+rep, mergedD.weighted.2021, svymean)$Topic6[2]
means.graph$D6.se[6]=svyby(~Topic6, ~dem+rep, mergedD.weighted.2021, svymean)$se[2]

means.graph$D6[7]=mean(mergedD$Topic6[mergedD$rep==1&mergedD$year2021==1], na.rm=T)
means.graph$D6.se[7]=std.error(mergedD$Topic6[mergedD$rep==1&mergedD$year2021==1])
means.graph$D6[8]=mean(mergedD$Topic6[mergedD$dem==1&mergedD$year2021==1], na.rm=T)
means.graph$D6.se[8]=std.error(mergedD$Topic6[mergedD$dem==1&mergedD$year2021==1])

#DTopic7
means.graph$D7=NA
means.graph$D7.se=NA
means.graph$D7[1]=svyby(~Topic7, ~dem+rep, mergedD.weighted.2016, svymean)$Topic7[3]
means.graph$D7.se[1]=svyby(~Topic7, ~dem+rep, mergedD.weighted.2016, svymean)$se[3]
means.graph$D7[2]=svyby(~Topic7, ~dem+rep, mergedD.weighted.2016, svymean)$Topic7[2]
means.graph$D7.se[2]=svyby(~Topic7, ~dem+rep, mergedD.weighted.2016, svymean)$se[2]

means.graph$D7[3]=mean(mergedD$Topic7[mergedD$rep==1&mergedD$year2021==0], na.rm=T)
means.graph$D7.se[3]=std.error(mergedD$Topic7[mergedD$rep==1&mergedD$year2021==0])
means.graph$D7[4]=mean(mergedD$Topic7[mergedD$dem==1&mergedD$year2021==0], na.rm=T)
means.graph$D7.se[4]=std.error(mergedD$Topic7[mergedD$dem==1&mergedD$year2021==0])

means.graph$D7[5]=svyby(~Topic7, ~dem+rep, mergedD.weighted.2021, svymean)$Topic7[3]
means.graph$D7.se[5]=svyby(~Topic7, ~dem+rep, mergedD.weighted.2021, svymean)$se[3]
means.graph$D7[6]=svyby(~Topic7, ~dem+rep, mergedD.weighted.2021, svymean)$Topic7[2]
means.graph$D7.se[6]=svyby(~Topic7, ~dem+rep, mergedD.weighted.2021, svymean)$se[2]

means.graph$D7[7]=mean(mergedD$Topic7[mergedD$rep==1&mergedD$year2021==1], na.rm=T)
means.graph$D7.se[7]=std.error(mergedD$Topic7[mergedD$rep==1&mergedD$year2021==1])
means.graph$D7[8]=mean(mergedD$Topic7[mergedD$dem==1&mergedD$year2021==1], na.rm=T)
means.graph$D7.se[8]=std.error(mergedD$Topic7[mergedD$dem==1&mergedD$year2021==1])

#DTopic8
means.graph$D8=NA
means.graph$D8.se=NA
means.graph$D8[1]=svyby(~Topic8, ~dem+rep, mergedD.weighted.2016, svymean)$Topic8[3]
means.graph$D8.se[1]=svyby(~Topic8, ~dem+rep, mergedD.weighted.2016, svymean)$se[3]
means.graph$D8[2]=svyby(~Topic8, ~dem+rep, mergedD.weighted.2016, svymean)$Topic8[2]
means.graph$D8.se[2]=svyby(~Topic8, ~dem+rep, mergedD.weighted.2016, svymean)$se[2]

means.graph$D8[3]=mean(mergedD$Topic8[mergedD$rep==1&mergedD$year2021==0], na.rm=T)
means.graph$D8.se[3]=std.error(mergedD$Topic8[mergedD$rep==1&mergedD$year2021==0])
means.graph$D8[4]=mean(mergedD$Topic8[mergedD$dem==1&mergedD$year2021==0], na.rm=T)
means.graph$D8.se[4]=std.error(mergedD$Topic8[mergedD$dem==1&mergedD$year2021==0])

means.graph$D8[5]=svyby(~Topic8, ~dem+rep, mergedD.weighted.2021, svymean)$Topic8[3]
means.graph$D8.se[5]=svyby(~Topic8, ~dem+rep, mergedD.weighted.2021, svymean)$se[3]
means.graph$D8[6]=svyby(~Topic8, ~dem+rep, mergedD.weighted.2021, svymean)$Topic8[2]
means.graph$D8.se[6]=svyby(~Topic8, ~dem+rep, mergedD.weighted.2021, svymean)$se[2]

means.graph$D8[7]=mean(mergedD$Topic8[mergedD$rep==1&mergedD$year2021==1], na.rm=T)
means.graph$D8.se[7]=std.error(mergedD$Topic8[mergedD$rep==1&mergedD$year2021==1])
means.graph$D8[8]=mean(mergedD$Topic8[mergedD$dem==1&mergedD$year2021==1], na.rm=T)
means.graph$D8.se[8]=std.error(mergedD$Topic8[mergedD$dem==1&mergedD$year2021==1])

#Republican topics
#R Topic 1
means.graph$R1=NA
means.graph$R1.se=NA
means.graph$R1[1]=svyby(~Topic1, ~dem+rep, mergedR.weighted.2016, svymean)$Topic1[3]
means.graph$R1.se[1]=svyby(~Topic1, ~dem+rep, mergedR.weighted.2016, svymean)$se[3]
means.graph$R1[2]=svyby(~Topic1, ~dem+rep, mergedR.weighted.2016, svymean)$Topic1[2]
means.graph$R1.se[2]=svyby(~Topic1, ~dem+rep, mergedR.weighted.2016, svymean)$se[2]

means.graph$R1[3]=mean(mergedR$Topic1[mergedR$rep==1&mergedR$year2021==0], na.rm=T)
means.graph$R1.se[3]=std.error(mergedR$Topic1[mergedR$rep==1&mergedR$year2021==0])
means.graph$R1[4]=mean(mergedR$Topic1[mergedR$dem==1&mergedR$year2021==0], na.rm=T)
means.graph$R1.se[4]=std.error(mergedR$Topic1[mergedR$dem==1&mergedR$year2021==0])

means.graph$R1[5]=svyby(~Topic1, ~dem+rep, mergedR.weighted.2021, svymean)$Topic1[3]
means.graph$R1.se[5]=svyby(~Topic1, ~dem+rep, mergedR.weighted.2021, svymean)$se[3]
means.graph$R1[6]=svyby(~Topic1, ~dem+rep, mergedR.weighted.2021, svymean)$Topic1[2]
means.graph$R1.se[6]=svyby(~Topic1, ~dem+rep, mergedR.weighted.2021, svymean)$se[2]

means.graph$R1[7]=mean(mergedR$Topic1[mergedR$rep==1&mergedR$year2021==1], na.rm=T)
means.graph$R1.se[7]=std.error(mergedR$Topic1[mergedR$rep==1&mergedR$year2021==1])
means.graph$R1[8]=mean(mergedR$Topic1[mergedR$dem==1&mergedR$year2021==1], na.rm=T)
means.graph$R1.se[8]=std.error(mergedR$Topic1[mergedR$dem==1&mergedR$year2021==1])

#RTopic2
means.graph$R2=NA
means.graph$R2.se=NA
means.graph$R2[1]=svyby(~Topic2, ~dem+rep, mergedR.weighted.2016, svymean)$Topic2[3]
means.graph$R2.se[1]=svyby(~Topic2, ~dem+rep, mergedR.weighted.2016, svymean)$se[3]
means.graph$R2[2]=svyby(~Topic2, ~dem+rep, mergedR.weighted.2016, svymean)$Topic2[2]
means.graph$R2.se[2]=svyby(~Topic2, ~dem+rep, mergedR.weighted.2016, svymean)$se[2]

means.graph$R2[3]=mean(mergedR$Topic2[mergedR$rep==1&mergedR$year2021==0], na.rm=T)
means.graph$R2.se[3]=std.error(mergedR$Topic2[mergedR$rep==1&mergedR$year2021==0])
means.graph$R2[4]=mean(mergedR$Topic2[mergedR$dem==1&mergedR$year2021==0], na.rm=T)
means.graph$R2.se[4]=std.error(mergedR$Topic2[mergedR$dem==1&mergedR$year2021==0])

means.graph$R2[5]=svyby(~Topic2, ~dem+rep, mergedR.weighted.2021, svymean)$Topic2[3]
means.graph$R2.se[5]=svyby(~Topic2, ~dem+rep, mergedR.weighted.2021, svymean)$se[3]
means.graph$R2[6]=svyby(~Topic2, ~dem+rep, mergedR.weighted.2021, svymean)$Topic2[2]
means.graph$R2.se[6]=svyby(~Topic2, ~dem+rep, mergedR.weighted.2021, svymean)$se[2]

means.graph$R2[7]=mean(mergedR$Topic2[mergedR$rep==1&mergedR$year2021==1], na.rm=T)
means.graph$R2.se[7]=std.error(mergedR$Topic2[mergedR$rep==1&mergedR$year2021==1])
means.graph$R2[8]=mean(mergedR$Topic2[mergedR$dem==1&mergedR$year2021==1], na.rm=T)
means.graph$R2.se[8]=std.error(mergedR$Topic2[mergedR$dem==1&mergedR$year2021==1])

#RTopic3
means.graph$R3=NA
means.graph$R3.se=NA
means.graph$R3[1]=svyby(~Topic3, ~dem+rep, mergedR.weighted.2016, svymean)$Topic3[3]
means.graph$R3.se[1]=svyby(~Topic3, ~dem+rep, mergedR.weighted.2016, svymean)$se[3]
means.graph$R3[2]=svyby(~Topic3, ~dem+rep, mergedR.weighted.2016, svymean)$Topic3[2]
means.graph$R3.se[2]=svyby(~Topic3, ~dem+rep, mergedR.weighted.2016, svymean)$se[2]

means.graph$R3[3]=mean(mergedR$Topic3[mergedR$rep==1&mergedR$year2021==0], na.rm=T)
means.graph$R3.se[3]=std.error(mergedR$Topic3[mergedR$rep==1&mergedR$year2021==0])
means.graph$R3[4]=mean(mergedR$Topic3[mergedR$dem==1&mergedR$year2021==0], na.rm=T)
means.graph$R3.se[4]=std.error(mergedR$Topic3[mergedR$dem==1&mergedR$year2021==0])

means.graph$R3[5]=svyby(~Topic3, ~dem+rep, mergedR.weighted.2021, svymean)$Topic3[3]
means.graph$R3.se[5]=svyby(~Topic3, ~dem+rep, mergedR.weighted.2021, svymean)$se[3]
means.graph$R3[6]=svyby(~Topic3, ~dem+rep, mergedR.weighted.2021, svymean)$Topic3[2]
means.graph$R3.se[6]=svyby(~Topic3, ~dem+rep, mergedR.weighted.2021, svymean)$se[2]

means.graph$R3[7]=mean(mergedR$Topic3[mergedR$rep==1&mergedR$year2021==1], na.rm=T)
means.graph$R3.se[7]=std.error(mergedR$Topic3[mergedR$rep==1&mergedR$year2021==1])
means.graph$R3[8]=mean(mergedR$Topic3[mergedR$dem==1&mergedR$year2021==1], na.rm=T)
means.graph$R3.se[8]=std.error(mergedR$Topic3[mergedR$dem==1&mergedR$year2021==1])

#RTopic4
means.graph$R4=NA
means.graph$R4.se=NA
means.graph$R4[1]=svyby(~Topic4, ~dem+rep, mergedR.weighted.2016, svymean)$Topic4[3]
means.graph$R4.se[1]=svyby(~Topic4, ~dem+rep, mergedR.weighted.2016, svymean)$se[3]
means.graph$R4[2]=svyby(~Topic4, ~dem+rep, mergedR.weighted.2016, svymean)$Topic4[2]
means.graph$R4.se[2]=svyby(~Topic4, ~dem+rep, mergedR.weighted.2016, svymean)$se[2]

means.graph$R4[3]=mean(mergedR$Topic4[mergedR$rep==1&mergedR$year2021==0], na.rm=T)
means.graph$R4.se[3]=std.error(mergedR$Topic4[mergedR$rep==1&mergedR$year2021==0])
means.graph$R4[4]=mean(mergedR$Topic4[mergedR$dem==1&mergedR$year2021==0], na.rm=T)
means.graph$R4.se[4]=std.error(mergedR$Topic4[mergedR$dem==1&mergedR$year2021==0])

means.graph$R4[5]=svyby(~Topic4, ~dem+rep, mergedR.weighted.2021, svymean)$Topic4[3]
means.graph$R4.se[5]=svyby(~Topic4, ~dem+rep, mergedR.weighted.2021, svymean)$se[3]
means.graph$R4[6]=svyby(~Topic4, ~dem+rep, mergedR.weighted.2021, svymean)$Topic4[2]
means.graph$R4.se[6]=svyby(~Topic4, ~dem+rep, mergedR.weighted.2021, svymean)$se[2]

means.graph$R4[7]=mean(mergedR$Topic4[mergedR$rep==1&mergedR$year2021==1], na.rm=T)
means.graph$R4.se[7]=std.error(mergedR$Topic4[mergedR$rep==1&mergedR$year2021==1])
means.graph$R4[8]=mean(mergedR$Topic4[mergedR$dem==1&mergedR$year2021==1], na.rm=T)
means.graph$R4.se[8]=std.error(mergedR$Topic4[mergedR$dem==1&mergedR$year2021==1])

#RTopic5
means.graph$R5=NA
means.graph$R5.se=NA
means.graph$R5[1]=svyby(~Topic5, ~dem+rep, mergedR.weighted.2016, svymean)$Topic5[3]
means.graph$R5.se[1]=svyby(~Topic5, ~dem+rep, mergedR.weighted.2016, svymean)$se[3]
means.graph$R5[2]=svyby(~Topic5, ~dem+rep, mergedR.weighted.2016, svymean)$Topic5[2]
means.graph$R5.se[2]=svyby(~Topic5, ~dem+rep, mergedR.weighted.2016, svymean)$se[2]

means.graph$R5[3]=mean(mergedR$Topic5[mergedR$rep==1&mergedR$year2021==0], na.rm=T)
means.graph$R5.se[3]=std.error(mergedR$Topic5[mergedR$rep==1&mergedR$year2021==0])
means.graph$R5[4]=mean(mergedR$Topic5[mergedR$dem==1&mergedR$year2021==0], na.rm=T)
means.graph$R5.se[4]=std.error(mergedR$Topic5[mergedR$dem==1&mergedR$year2021==0])

means.graph$R5[5]=svyby(~Topic5, ~dem+rep, mergedR.weighted.2021, svymean)$Topic5[3]
means.graph$R5.se[5]=svyby(~Topic5, ~dem+rep, mergedR.weighted.2021, svymean)$se[3]
means.graph$R5[6]=svyby(~Topic5, ~dem+rep, mergedR.weighted.2021, svymean)$Topic5[2]
means.graph$R5.se[6]=svyby(~Topic5, ~dem+rep, mergedR.weighted.2021, svymean)$se[2]

means.graph$R5[7]=mean(mergedR$Topic5[mergedR$rep==1&mergedR$year2021==1], na.rm=T)
means.graph$R5.se[7]=std.error(mergedR$Topic5[mergedR$rep==1&mergedR$year2021==1])
means.graph$R5[8]=mean(mergedR$Topic5[mergedR$dem==1&mergedR$year2021==1], na.rm=T)
means.graph$R5.se[8]=std.error(mergedR$Topic5[mergedR$dem==1&mergedR$year2021==1])

#RTopic6
means.graph$R6=NA
means.graph$R6.se=NA
means.graph$R6[1]=svyby(~Topic6, ~dem+rep, mergedR.weighted.2016, svymean)$Topic6[3]
means.graph$R6.se[1]=svyby(~Topic6, ~dem+rep, mergedR.weighted.2016, svymean)$se[3]
means.graph$R6[2]=svyby(~Topic6, ~dem+rep, mergedR.weighted.2016, svymean)$Topic6[2]
means.graph$R6.se[2]=svyby(~Topic6, ~dem+rep, mergedR.weighted.2016, svymean)$se[2]

means.graph$R6[3]=mean(mergedR$Topic6[mergedR$rep==1&mergedR$year2021==0], na.rm=T)
means.graph$R6.se[3]=std.error(mergedR$Topic6[mergedR$rep==1&mergedR$year2021==0])
means.graph$R6[4]=mean(mergedR$Topic6[mergedR$dem==1&mergedR$year2021==0], na.rm=T)
means.graph$R6.se[4]=std.error(mergedR$Topic6[mergedR$dem==1&mergedR$year2021==0])

means.graph$R6[5]=svyby(~Topic6, ~dem+rep, mergedR.weighted.2021, svymean)$Topic6[3]
means.graph$R6.se[5]=svyby(~Topic6, ~dem+rep, mergedR.weighted.2021, svymean)$se[3]
means.graph$R6[6]=svyby(~Topic6, ~dem+rep, mergedR.weighted.2021, svymean)$Topic6[2]
means.graph$R6.se[6]=svyby(~Topic6, ~dem+rep, mergedR.weighted.2021, svymean)$se[2]

means.graph$R6[7]=mean(mergedR$Topic6[mergedR$rep==1&mergedR$year2021==1], na.rm=T)
means.graph$R6.se[7]=std.error(mergedR$Topic6[mergedR$rep==1&mergedR$year2021==1])
means.graph$R6[8]=mean(mergedR$Topic6[mergedR$dem==1&mergedR$year2021==1], na.rm=T)
means.graph$R6.se[8]=std.error(mergedR$Topic6[mergedR$dem==1&mergedR$year2021==1])

#RTopic7
means.graph$R7=NA
means.graph$R7.se=NA
means.graph$R7[1]=svyby(~Topic7, ~dem+rep, mergedR.weighted.2016, svymean)$Topic7[3]
means.graph$R7.se[1]=svyby(~Topic7, ~dem+rep, mergedR.weighted.2016, svymean)$se[3]
means.graph$R7[2]=svyby(~Topic7, ~dem+rep, mergedR.weighted.2016, svymean)$Topic7[2]
means.graph$R7.se[2]=svyby(~Topic7, ~dem+rep, mergedR.weighted.2016, svymean)$se[2]

means.graph$R7[3]=mean(mergedR$Topic7[mergedR$rep==1&mergedR$year2021==0], na.rm=T)
means.graph$R7.se[3]=std.error(mergedR$Topic7[mergedR$rep==1&mergedR$year2021==0])
means.graph$R7[4]=mean(mergedR$Topic7[mergedR$dem==1&mergedR$year2021==0], na.rm=T)
means.graph$R7.se[4]=std.error(mergedR$Topic7[mergedR$dem==1&mergedR$year2021==0])

means.graph$R7[5]=svyby(~Topic7, ~dem+rep, mergedR.weighted.2021, svymean)$Topic7[3]
means.graph$R7.se[5]=svyby(~Topic7, ~dem+rep, mergedR.weighted.2021, svymean)$se[3]
means.graph$R7[6]=svyby(~Topic7, ~dem+rep, mergedR.weighted.2021, svymean)$Topic7[2]
means.graph$R7.se[6]=svyby(~Topic7, ~dem+rep, mergedR.weighted.2021, svymean)$se[2]

means.graph$R7[7]=mean(mergedR$Topic7[mergedR$rep==1&mergedR$year2021==1], na.rm=T)
means.graph$R7.se[7]=std.error(mergedR$Topic7[mergedR$rep==1&mergedR$year2021==1])
means.graph$R7[8]=mean(mergedR$Topic7[mergedR$dem==1&mergedR$year2021==1], na.rm=T)
means.graph$R7.se[8]=std.error(mergedR$Topic7[mergedR$dem==1&mergedR$year2021==1])

#RTopic8
means.graph$R8=NA
means.graph$R8.se=NA
means.graph$R8[1]=svyby(~Topic8, ~dem+rep, mergedR.weighted.2016, svymean)$Topic8[3]
means.graph$R8.se[1]=svyby(~Topic8, ~dem+rep, mergedR.weighted.2016, svymean)$se[3]
means.graph$R8[2]=svyby(~Topic8, ~dem+rep, mergedR.weighted.2016, svymean)$Topic8[2]
means.graph$R8.se[2]=svyby(~Topic8, ~dem+rep, mergedR.weighted.2016, svymean)$se[2]

means.graph$R8[3]=mean(mergedR$Topic8[mergedR$rep==1&mergedR$year2021==0], na.rm=T)
means.graph$R8.se[3]=std.error(mergedR$Topic8[mergedR$rep==1&mergedR$year2021==0])
means.graph$R8[4]=mean(mergedR$Topic8[mergedR$dem==1&mergedR$year2021==0], na.rm=T)
means.graph$R8.se[4]=std.error(mergedR$Topic8[mergedR$dem==1&mergedR$year2021==0])

means.graph$R8[5]=svyby(~Topic8, ~dem+rep, mergedR.weighted.2021, svymean)$Topic8[3]
means.graph$R8.se[5]=svyby(~Topic8, ~dem+rep, mergedR.weighted.2021, svymean)$se[3]
means.graph$R8[6]=svyby(~Topic8, ~dem+rep, mergedR.weighted.2021, svymean)$Topic8[2]
means.graph$R8.se[6]=svyby(~Topic8, ~dem+rep, mergedR.weighted.2021, svymean)$se[2]

means.graph$R8[7]=mean(mergedR$Topic8[mergedR$rep==1&mergedR$year2021==1], na.rm=T)
means.graph$R8.se[7]=std.error(mergedR$Topic8[mergedR$rep==1&mergedR$year2021==1])
means.graph$R8[8]=mean(mergedR$Topic8[mergedR$dem==1&mergedR$year2021==1], na.rm=T)
means.graph$R8.se[8]=std.error(mergedR$Topic8[mergedR$dem==1&mergedR$year2021==1])

##Weighted results##
#Make this into a similar structure as Figure 1
party=c(rep(c("Stereotypes of Republicans", "Stereotypes of Republicans"),16),
        rep(c("Stereotypes of Democrats", "Stereotypes of Democrats"),16)
)
respondent=c(rep("Republican respondents",16), 
             rep("Democratic respondents",16),
             rep("Republican respondents",16), 
             rep("Democratic respondents",16))

topic=c(1,1,
        2,2,
        3,3,
        4,4,
        5,5,
        6,6,
        7,7,
        8,8,
        1,1,
        2,2,
        3,3,
        4,4,
        5,5,
        6,6,
        7,7,
        8,8,
        1,1,
        2,2,
        3,3,
        4,4,
        5,5,
        6,6,
        7,7,
        8,8,
        1,1,
        2,2,
        3,3,
        4,4,
        5,5,
        6,6,
        7,7,
        8,8
)

bar=c(means.graph$R1[means.graph$year==2016&means.graph$party_temp=="Republican"&means.graph$weights=="Weighted"],
      means.graph$R1[means.graph$year==2021&means.graph$party_temp=="Republican"&means.graph$weights=="Weighted"],
      means.graph$R2[means.graph$year==2016&means.graph$party_temp=="Republican"&means.graph$weights=="Weighted"],
      means.graph$R2[means.graph$year==2021&means.graph$party_temp=="Republican"&means.graph$weights=="Weighted"],
      means.graph$R3[means.graph$year==2016&means.graph$party_temp=="Republican"&means.graph$weights=="Weighted"],
      means.graph$R3[means.graph$year==2021&means.graph$party_temp=="Republican"&means.graph$weights=="Weighted"],
      means.graph$R4[means.graph$year==2016&means.graph$party_temp=="Republican"&means.graph$weights=="Weighted"],
      means.graph$R4[means.graph$year==2021&means.graph$party_temp=="Republican"&means.graph$weights=="Weighted"],
      means.graph$R5[means.graph$year==2016&means.graph$party_temp=="Republican"&means.graph$weights=="Weighted"],
      means.graph$R5[means.graph$year==2021&means.graph$party_temp=="Republican"&means.graph$weights=="Weighted"],
      means.graph$R6[means.graph$year==2016&means.graph$party_temp=="Republican"&means.graph$weights=="Weighted"],
      means.graph$R6[means.graph$year==2021&means.graph$party_temp=="Republican"&means.graph$weights=="Weighted"],
      means.graph$R7[means.graph$year==2016&means.graph$party_temp=="Republican"&means.graph$weights=="Weighted"],
      means.graph$R7[means.graph$year==2021&means.graph$party_temp=="Republican"&means.graph$weights=="Weighted"],
      means.graph$R8[means.graph$year==2016&means.graph$party_temp=="Republican"&means.graph$weights=="Weighted"],
      means.graph$R8[means.graph$year==2021&means.graph$party_temp=="Republican"&means.graph$weights=="Weighted"],
      means.graph$R1[means.graph$year==2016&means.graph$party_temp=="Democrat"&means.graph$weights=="Weighted"],
      means.graph$R1[means.graph$year==2021&means.graph$party_temp=="Democrat"&means.graph$weights=="Weighted"],
      means.graph$R2[means.graph$year==2016&means.graph$party_temp=="Democrat"&means.graph$weights=="Weighted"],
      means.graph$R2[means.graph$year==2021&means.graph$party_temp=="Democrat"&means.graph$weights=="Weighted"],
      means.graph$R3[means.graph$year==2016&means.graph$party_temp=="Democrat"&means.graph$weights=="Weighted"],
      means.graph$R3[means.graph$year==2021&means.graph$party_temp=="Democrat"&means.graph$weights=="Weighted"],
      means.graph$R4[means.graph$year==2016&means.graph$party_temp=="Democrat"&means.graph$weights=="Weighted"],
      means.graph$R4[means.graph$year==2021&means.graph$party_temp=="Democrat"&means.graph$weights=="Weighted"],
      means.graph$R5[means.graph$year==2016&means.graph$party_temp=="Democrat"&means.graph$weights=="Weighted"],
      means.graph$R5[means.graph$year==2021&means.graph$party_temp=="Democrat"&means.graph$weights=="Weighted"],
      means.graph$R6[means.graph$year==2016&means.graph$party_temp=="Democrat"&means.graph$weights=="Weighted"],
      means.graph$R6[means.graph$year==2021&means.graph$party_temp=="Democrat"&means.graph$weights=="Weighted"],
      means.graph$R7[means.graph$year==2016&means.graph$party_temp=="Democrat"&means.graph$weights=="Weighted"],
      means.graph$R7[means.graph$year==2021&means.graph$party_temp=="Democrat"&means.graph$weights=="Weighted"],
      means.graph$R8[means.graph$year==2016&means.graph$party_temp=="Democrat"&means.graph$weights=="Weighted"],
      means.graph$R8[means.graph$year==2021&means.graph$party_temp=="Democrat"&means.graph$weights=="Weighted"],
      means.graph$D1[means.graph$year==2016&means.graph$party_temp=="Republican"&means.graph$weights=="Weighted"],
      means.graph$D1[means.graph$year==2021&means.graph$party_temp=="Republican"&means.graph$weights=="Weighted"],
      means.graph$D2[means.graph$year==2016&means.graph$party_temp=="Republican"&means.graph$weights=="Weighted"],
      means.graph$D2[means.graph$year==2021&means.graph$party_temp=="Republican"&means.graph$weights=="Weighted"],
      means.graph$D3[means.graph$year==2016&means.graph$party_temp=="Republican"&means.graph$weights=="Weighted"],
      means.graph$D3[means.graph$year==2021&means.graph$party_temp=="Republican"&means.graph$weights=="Weighted"],
      means.graph$D4[means.graph$year==2016&means.graph$party_temp=="Republican"&means.graph$weights=="Weighted"],
      means.graph$D4[means.graph$year==2021&means.graph$party_temp=="Republican"&means.graph$weights=="Weighted"],
      means.graph$D5[means.graph$year==2016&means.graph$party_temp=="Republican"&means.graph$weights=="Weighted"],
      means.graph$D5[means.graph$year==2021&means.graph$party_temp=="Republican"&means.graph$weights=="Weighted"],
      means.graph$D6[means.graph$year==2016&means.graph$party_temp=="Republican"&means.graph$weights=="Weighted"],
      means.graph$D6[means.graph$year==2021&means.graph$party_temp=="Republican"&means.graph$weights=="Weighted"],
      means.graph$D7[means.graph$year==2016&means.graph$party_temp=="Republican"&means.graph$weights=="Weighted"],
      means.graph$D7[means.graph$year==2021&means.graph$party_temp=="Republican"&means.graph$weights=="Weighted"],
      means.graph$D8[means.graph$year==2016&means.graph$party_temp=="Republican"&means.graph$weights=="Weighted"],
      means.graph$D8[means.graph$year==2021&means.graph$party_temp=="Republican"&means.graph$weights=="Weighted"],
      means.graph$D1[means.graph$year==2016&means.graph$party_temp=="Democrat"&means.graph$weights=="Weighted"],
      means.graph$D1[means.graph$year==2021&means.graph$party_temp=="Democrat"&means.graph$weights=="Weighted"],
      means.graph$D2[means.graph$year==2016&means.graph$party_temp=="Democrat"&means.graph$weights=="Weighted"],
      means.graph$D2[means.graph$year==2021&means.graph$party_temp=="Democrat"&means.graph$weights=="Weighted"],
      means.graph$D3[means.graph$year==2016&means.graph$party_temp=="Democrat"&means.graph$weights=="Weighted"],
      means.graph$D3[means.graph$year==2021&means.graph$party_temp=="Democrat"&means.graph$weights=="Weighted"],
      means.graph$D4[means.graph$year==2016&means.graph$party_temp=="Democrat"&means.graph$weights=="Weighted"],
      means.graph$D4[means.graph$year==2021&means.graph$party_temp=="Democrat"&means.graph$weights=="Weighted"],
      means.graph$D5[means.graph$year==2016&means.graph$party_temp=="Democrat"&means.graph$weights=="Weighted"],
      means.graph$D5[means.graph$year==2021&means.graph$party_temp=="Democrat"&means.graph$weights=="Weighted"],
      means.graph$D6[means.graph$year==2016&means.graph$party_temp=="Democrat"&means.graph$weights=="Weighted"],
      means.graph$D6[means.graph$year==2021&means.graph$party_temp=="Democrat"&means.graph$weights=="Weighted"],
      means.graph$D7[means.graph$year==2016&means.graph$party_temp=="Democrat"&means.graph$weights=="Weighted"],
      means.graph$D7[means.graph$year==2021&means.graph$party_temp=="Democrat"&means.graph$weights=="Weighted"],
      means.graph$D8[means.graph$year==2016&means.graph$party_temp=="Democrat"&means.graph$weights=="Weighted"],
      means.graph$D8[means.graph$year==2021&means.graph$party_temp=="Democrat"&means.graph$weights=="Weighted"]
      
)

se=c(
  means.graph$R1.se[means.graph$year==2016&means.graph$party_temp=="Republican"&means.graph$weights=="Weighted"],
  means.graph$R1.se[means.graph$year==2021&means.graph$party_temp=="Republican"&means.graph$weights=="Weighted"],
  means.graph$R2.se[means.graph$year==2016&means.graph$party_temp=="Republican"&means.graph$weights=="Weighted"],
  means.graph$R2.se[means.graph$year==2021&means.graph$party_temp=="Republican"&means.graph$weights=="Weighted"],
  means.graph$R3.se[means.graph$year==2016&means.graph$party_temp=="Republican"&means.graph$weights=="Weighted"],
  means.graph$R3.se[means.graph$year==2021&means.graph$party_temp=="Republican"&means.graph$weights=="Weighted"],
  means.graph$R4.se[means.graph$year==2016&means.graph$party_temp=="Republican"&means.graph$weights=="Weighted"],
  means.graph$R4.se[means.graph$year==2021&means.graph$party_temp=="Republican"&means.graph$weights=="Weighted"],
  means.graph$R5.se[means.graph$year==2016&means.graph$party_temp=="Republican"&means.graph$weights=="Weighted"],
  means.graph$R5.se[means.graph$year==2021&means.graph$party_temp=="Republican"&means.graph$weights=="Weighted"],
  means.graph$R6.se[means.graph$year==2016&means.graph$party_temp=="Republican"&means.graph$weights=="Weighted"],
  means.graph$R6.se[means.graph$year==2021&means.graph$party_temp=="Republican"&means.graph$weights=="Weighted"],
  means.graph$R7.se[means.graph$year==2016&means.graph$party_temp=="Republican"&means.graph$weights=="Weighted"],
  means.graph$R7.se[means.graph$year==2021&means.graph$party_temp=="Republican"&means.graph$weights=="Weighted"],
  means.graph$R8.se[means.graph$year==2016&means.graph$party_temp=="Republican"&means.graph$weights=="Weighted"],
  means.graph$R8.se[means.graph$year==2021&means.graph$party_temp=="Republican"&means.graph$weights=="Weighted"],
  means.graph$R1.se[means.graph$year==2016&means.graph$party_temp=="Democrat"&means.graph$weights=="Weighted"],
  means.graph$R1.se[means.graph$year==2021&means.graph$party_temp=="Democrat"&means.graph$weights=="Weighted"],
  means.graph$R2.se[means.graph$year==2016&means.graph$party_temp=="Democrat"&means.graph$weights=="Weighted"],
  means.graph$R2.se[means.graph$year==2021&means.graph$party_temp=="Democrat"&means.graph$weights=="Weighted"],
  means.graph$R3.se[means.graph$year==2016&means.graph$party_temp=="Democrat"&means.graph$weights=="Weighted"],
  means.graph$R3.se[means.graph$year==2021&means.graph$party_temp=="Democrat"&means.graph$weights=="Weighted"],
  means.graph$R4.se[means.graph$year==2016&means.graph$party_temp=="Democrat"&means.graph$weights=="Weighted"],
  means.graph$R4.se[means.graph$year==2021&means.graph$party_temp=="Democrat"&means.graph$weights=="Weighted"],
  means.graph$R5.se[means.graph$year==2016&means.graph$party_temp=="Democrat"&means.graph$weights=="Weighted"],
  means.graph$R5.se[means.graph$year==2021&means.graph$party_temp=="Democrat"&means.graph$weights=="Weighted"],
  means.graph$R6.se[means.graph$year==2016&means.graph$party_temp=="Democrat"&means.graph$weights=="Weighted"],
  means.graph$R6.se[means.graph$year==2021&means.graph$party_temp=="Democrat"&means.graph$weights=="Weighted"],
  means.graph$R7.se[means.graph$year==2016&means.graph$party_temp=="Democrat"&means.graph$weights=="Weighted"],
  means.graph$R7.se[means.graph$year==2021&means.graph$party_temp=="Democrat"&means.graph$weights=="Weighted"],
  means.graph$R8.se[means.graph$year==2016&means.graph$party_temp=="Democrat"&means.graph$weights=="Weighted"],
  means.graph$R8.se[means.graph$year==2021&means.graph$party_temp=="Democrat"&means.graph$weights=="Weighted"],
  means.graph$D1.se[means.graph$year==2016&means.graph$party_temp=="Republican"&means.graph$weights=="Weighted"],
  means.graph$D1.se[means.graph$year==2021&means.graph$party_temp=="Republican"&means.graph$weights=="Weighted"],
  means.graph$D2.se[means.graph$year==2016&means.graph$party_temp=="Republican"&means.graph$weights=="Weighted"],
  means.graph$D2.se[means.graph$year==2021&means.graph$party_temp=="Republican"&means.graph$weights=="Weighted"],
  means.graph$D3.se[means.graph$year==2016&means.graph$party_temp=="Republican"&means.graph$weights=="Weighted"],
  means.graph$D3.se[means.graph$year==2021&means.graph$party_temp=="Republican"&means.graph$weights=="Weighted"],
  means.graph$D4.se[means.graph$year==2016&means.graph$party_temp=="Republican"&means.graph$weights=="Weighted"],
  means.graph$D4.se[means.graph$year==2021&means.graph$party_temp=="Republican"&means.graph$weights=="Weighted"],
  means.graph$D5.se[means.graph$year==2016&means.graph$party_temp=="Republican"&means.graph$weights=="Weighted"],
  means.graph$D5.se[means.graph$year==2021&means.graph$party_temp=="Republican"&means.graph$weights=="Weighted"],
  means.graph$D6.se[means.graph$year==2016&means.graph$party_temp=="Republican"&means.graph$weights=="Weighted"],
  means.graph$D6.se[means.graph$year==2021&means.graph$party_temp=="Republican"&means.graph$weights=="Weighted"],
  means.graph$D7.se[means.graph$year==2016&means.graph$party_temp=="Republican"&means.graph$weights=="Weighted"],
  means.graph$D7.se[means.graph$year==2021&means.graph$party_temp=="Republican"&means.graph$weights=="Weighted"],
  means.graph$D8.se[means.graph$year==2016&means.graph$party_temp=="Republican"&means.graph$weights=="Weighted"],
  means.graph$D8.se[means.graph$year==2021&means.graph$party_temp=="Republican"&means.graph$weights=="Weighted"],
  means.graph$D1.se[means.graph$year==2016&means.graph$party_temp=="Democrat"&means.graph$weights=="Weighted"],
  means.graph$D1.se[means.graph$year==2021&means.graph$party_temp=="Democrat"&means.graph$weights=="Weighted"],
  means.graph$D2.se[means.graph$year==2016&means.graph$party_temp=="Democrat"&means.graph$weights=="Weighted"],
  means.graph$D2.se[means.graph$year==2021&means.graph$party_temp=="Democrat"&means.graph$weights=="Weighted"],
  means.graph$D3.se[means.graph$year==2016&means.graph$party_temp=="Democrat"&means.graph$weights=="Weighted"],
  means.graph$D3.se[means.graph$year==2021&means.graph$party_temp=="Democrat"&means.graph$weights=="Weighted"],
  means.graph$D4.se[means.graph$year==2016&means.graph$party_temp=="Democrat"&means.graph$weights=="Weighted"],
  means.graph$D4.se[means.graph$year==2021&means.graph$party_temp=="Democrat"&means.graph$weights=="Weighted"],
  means.graph$D5.se[means.graph$year==2016&means.graph$party_temp=="Democrat"&means.graph$weights=="Weighted"],
  means.graph$D5.se[means.graph$year==2021&means.graph$party_temp=="Democrat"&means.graph$weights=="Weighted"],
  means.graph$D6.se[means.graph$year==2016&means.graph$party_temp=="Democrat"&means.graph$weights=="Weighted"],
  means.graph$D6.se[means.graph$year==2021&means.graph$party_temp=="Democrat"&means.graph$weights=="Weighted"],
  means.graph$D7.se[means.graph$year==2016&means.graph$party_temp=="Democrat"&means.graph$weights=="Weighted"],
  means.graph$D7.se[means.graph$year==2021&means.graph$party_temp=="Democrat"&means.graph$weights=="Weighted"],
  means.graph$D8.se[means.graph$year==2016&means.graph$party_temp=="Democrat"&means.graph$weights=="Weighted"],
  means.graph$D8.se[means.graph$year==2021&means.graph$party_temp=="Democrat"&means.graph$weights=="Weighted"]
)

year=c(rep(c(2016,2021),32)
)

#Add these together as one data object
t2=as.data.frame(cbind(party, respondent))
t2$topic=topic
t2$bar=bar
t2$year=year
t2$year=as.factor(year)
t2$se=se
t2$low=t2$bar-(1.96*t2$se)
t2$high=t2$bar+(1.96*t2$se)

#making the graph
bar2w=ggplot(data=t2[t2$party=="Stereotypes of Democrats"&t2$respondent=="Democratic respondents",], aes(x=topic, y=bar, fill=year, group=year))+
  geom_bar(position=position_dodge(), stat="identity")+
  geom_errorbar(aes(ymin=low, ymax=high),width=0.2, position=position_dodge((0.9)))+
  xlab("")+
  ylab("Democratic respondents")+
  ggtitle("")+
  theme(plot.caption=element_text(hjust=0))+
  scale_fill_grey()+
  scale_x_continuous(breaks = unique(t$topic),
                     labels=c("1. Bad People", "2. Nonresponse", "3. Liberal Coalition",
                              "4. Class Interests", "5. Dishonest and Lazy", "6. Smart and Honest",
                              "7. Insincere Positive", "8. Caring and Inclusive"))+
  scale_y_continuous(limits=c(-0.06,0.7))
bar2w=bar2w+coord_flip()+theme_bw()+theme(legend.position = "none")

bar2.1w=ggplot(data=t2[t2$party=="Stereotypes of Democrats"&t2$respondent=="Republican respondents",], aes(x=topic, y=bar, fill=year, group=year))+
  geom_bar(position=position_dodge(), stat="identity")+
  geom_errorbar(aes(ymin=low, ymax=high),width=0.2, position=position_dodge((0.9)))+
  xlab("")+
  ylab("Republican respondents")+
  ggtitle("")+
  theme(plot.caption=element_text(hjust=0))+
  scale_fill_grey()+
  scale_x_continuous(breaks = unique(t$topic),
                     labels=c("", "", "",
                              "", "", "",
                              "", ""))+
  scale_y_continuous(limits=c(-0.06,0.7))
bar2.1w=bar2.1w+coord_flip()+theme_bw()+
  theme(legend.position = "none")

bar2.2w=ggplot(data=t2[t2$party=="Stereotypes of Republicans"&t2$respondent=="Democratic respondents",], aes(x=topic, y=bar, fill=year, group=year))+
  geom_bar(position=position_dodge(), stat="identity")+
  geom_errorbar(aes(ymin=low, ymax=high),width=0.2, position=position_dodge((0.9)))+
  xlab("")+
  ylab("Democratic respondents")+
  ggtitle("")+
  theme(plot.caption=element_text(hjust=0))+
  scale_fill_grey()+
  scale_x_continuous(breaks = unique(t$topic),
                     labels=c("1. Nonresponse", "2. Caring Patriots", "3. Rich White Men",
                              "4. Selfish Bigots", "5. Traditional Coalition", "6. Class Interests",
                              "7. Religious \nFundamentalists", "8. Insincere Positive"))+
  scale_y_continuous(limits=c(-0.06,0.7))
bar2.2w=bar2.2w+coord_flip()+theme_bw()+
  theme(legend.position = "bottom",
        legend.key.size=unit(0.3, "cm"),
        legend.text=element_text(size=8),
        legend.margin=margin(0))+ theme(legend.title=element_blank())

bar2.3w=ggplot(data=t2[t2$party=="Stereotypes of Republicans"&t2$respondent=="Republican respondents",], aes(x=topic, y=bar, fill=year, group=year))+
  geom_bar(position=position_dodge(), stat="identity")+
  geom_errorbar(aes(ymin=low, ymax=high),width=0.2, position=position_dodge((0.9)))+
  xlab("")+
  ylab("Republican respondents")+
  ggtitle("")+
  labs(caption="Figure presents topic proportions along with 95 percent confidence intervals")+
  theme(plot.caption=element_text(hjust=0))+
  scale_fill_grey()+
  scale_x_continuous(breaks = unique(t$topic),
                     labels=c("", "", "",
                              "", "", "",
                              "", ""))+
  scale_y_continuous(limits=c(-0.06,0.7))
bar2.3w=bar2.3w+coord_flip()+theme_bw()+
  theme(legend.position = "none")

plot_grid(bar2w, bar2.1w, bar2.2w, bar2.3w, labels=c("Stereotypes of Democrats", "", "Stereotypes of Republicans", ""),
          rel_widths = c(1.1,0.9, 1.5, 1), rel_heights = c(1,1,1,0.85))

jpeg("Figure B.1-weighted-mean-final.jpeg", width=10, height=8, units="in", res=600)
plot_grid(bar2w, bar2.1w, bar2.2w, bar2.3w, labels=c("Stereotypes of Democrats", "", "Stereotypes of Republicans", ""),
          rel_widths = c(1.1,0.9, 1.5, 1), rel_heights = c(1,1,1,0.85))
dev.off()

#Figure B.2. Unweighted estimates
party=c(rep(c("Stereotypes of Republicans", "Stereotypes of Republicans"),16),
        rep(c("Stereotypes of Democrats", "Stereotypes of Democrats"),16)
)
respondent=c(rep("Republican respondents",16), 
             rep("Democratic respondents",16),
             rep("Republican respondents",16), 
             rep("Democratic respondents",16))

topic=c(1,1,
        2,2,
        3,3,
        4,4,
        5,5,
        6,6,
        7,7,
        8,8,
        1,1,
        2,2,
        3,3,
        4,4,
        5,5,
        6,6,
        7,7,
        8,8,
        1,1,
        2,2,
        3,3,
        4,4,
        5,5,
        6,6,
        7,7,
        8,8,
        1,1,
        2,2,
        3,3,
        4,4,
        5,5,
        6,6,
        7,7,
        8,8
)

bar=c(means.graph$R1[means.graph$year==2016&means.graph$party_temp=="Republican"&means.graph$weights=="Unweighted"],
      means.graph$R1[means.graph$year==2021&means.graph$party_temp=="Republican"&means.graph$weights=="Unweighted"],
      means.graph$R2[means.graph$year==2016&means.graph$party_temp=="Republican"&means.graph$weights=="Unweighted"],
      means.graph$R2[means.graph$year==2021&means.graph$party_temp=="Republican"&means.graph$weights=="Unweighted"],
      means.graph$R3[means.graph$year==2016&means.graph$party_temp=="Republican"&means.graph$weights=="Unweighted"],
      means.graph$R3[means.graph$year==2021&means.graph$party_temp=="Republican"&means.graph$weights=="Unweighted"],
      means.graph$R4[means.graph$year==2016&means.graph$party_temp=="Republican"&means.graph$weights=="Unweighted"],
      means.graph$R4[means.graph$year==2021&means.graph$party_temp=="Republican"&means.graph$weights=="Unweighted"],
      means.graph$R5[means.graph$year==2016&means.graph$party_temp=="Republican"&means.graph$weights=="Unweighted"],
      means.graph$R5[means.graph$year==2021&means.graph$party_temp=="Republican"&means.graph$weights=="Unweighted"],
      means.graph$R6[means.graph$year==2016&means.graph$party_temp=="Republican"&means.graph$weights=="Unweighted"],
      means.graph$R6[means.graph$year==2021&means.graph$party_temp=="Republican"&means.graph$weights=="Unweighted"],
      means.graph$R7[means.graph$year==2016&means.graph$party_temp=="Republican"&means.graph$weights=="Unweighted"],
      means.graph$R7[means.graph$year==2021&means.graph$party_temp=="Republican"&means.graph$weights=="Unweighted"],
      means.graph$R8[means.graph$year==2016&means.graph$party_temp=="Republican"&means.graph$weights=="Unweighted"],
      means.graph$R8[means.graph$year==2021&means.graph$party_temp=="Republican"&means.graph$weights=="Unweighted"],
      means.graph$R1[means.graph$year==2016&means.graph$party_temp=="Democrat"&means.graph$weights=="Unweighted"],
      means.graph$R1[means.graph$year==2021&means.graph$party_temp=="Democrat"&means.graph$weights=="Unweighted"],
      means.graph$R2[means.graph$year==2016&means.graph$party_temp=="Democrat"&means.graph$weights=="Unweighted"],
      means.graph$R2[means.graph$year==2021&means.graph$party_temp=="Democrat"&means.graph$weights=="Unweighted"],
      means.graph$R3[means.graph$year==2016&means.graph$party_temp=="Democrat"&means.graph$weights=="Unweighted"],
      means.graph$R3[means.graph$year==2021&means.graph$party_temp=="Democrat"&means.graph$weights=="Unweighted"],
      means.graph$R4[means.graph$year==2016&means.graph$party_temp=="Democrat"&means.graph$weights=="Unweighted"],
      means.graph$R4[means.graph$year==2021&means.graph$party_temp=="Democrat"&means.graph$weights=="Unweighted"],
      means.graph$R5[means.graph$year==2016&means.graph$party_temp=="Democrat"&means.graph$weights=="Unweighted"],
      means.graph$R5[means.graph$year==2021&means.graph$party_temp=="Democrat"&means.graph$weights=="Unweighted"],
      means.graph$R6[means.graph$year==2016&means.graph$party_temp=="Democrat"&means.graph$weights=="Unweighted"],
      means.graph$R6[means.graph$year==2021&means.graph$party_temp=="Democrat"&means.graph$weights=="Unweighted"],
      means.graph$R7[means.graph$year==2016&means.graph$party_temp=="Democrat"&means.graph$weights=="Unweighted"],
      means.graph$R7[means.graph$year==2021&means.graph$party_temp=="Democrat"&means.graph$weights=="Unweighted"],
      means.graph$R8[means.graph$year==2016&means.graph$party_temp=="Democrat"&means.graph$weights=="Unweighted"],
      means.graph$R8[means.graph$year==2021&means.graph$party_temp=="Democrat"&means.graph$weights=="Unweighted"],
      means.graph$D1[means.graph$year==2016&means.graph$party_temp=="Republican"&means.graph$weights=="Unweighted"],
      means.graph$D1[means.graph$year==2021&means.graph$party_temp=="Republican"&means.graph$weights=="Unweighted"],
      means.graph$D2[means.graph$year==2016&means.graph$party_temp=="Republican"&means.graph$weights=="Unweighted"],
      means.graph$D2[means.graph$year==2021&means.graph$party_temp=="Republican"&means.graph$weights=="Unweighted"],
      means.graph$D3[means.graph$year==2016&means.graph$party_temp=="Republican"&means.graph$weights=="Unweighted"],
      means.graph$D3[means.graph$year==2021&means.graph$party_temp=="Republican"&means.graph$weights=="Unweighted"],
      means.graph$D4[means.graph$year==2016&means.graph$party_temp=="Republican"&means.graph$weights=="Unweighted"],
      means.graph$D4[means.graph$year==2021&means.graph$party_temp=="Republican"&means.graph$weights=="Unweighted"],
      means.graph$D5[means.graph$year==2016&means.graph$party_temp=="Republican"&means.graph$weights=="Unweighted"],
      means.graph$D5[means.graph$year==2021&means.graph$party_temp=="Republican"&means.graph$weights=="Unweighted"],
      means.graph$D6[means.graph$year==2016&means.graph$party_temp=="Republican"&means.graph$weights=="Unweighted"],
      means.graph$D6[means.graph$year==2021&means.graph$party_temp=="Republican"&means.graph$weights=="Unweighted"],
      means.graph$D7[means.graph$year==2016&means.graph$party_temp=="Republican"&means.graph$weights=="Unweighted"],
      means.graph$D7[means.graph$year==2021&means.graph$party_temp=="Republican"&means.graph$weights=="Unweighted"],
      means.graph$D8[means.graph$year==2016&means.graph$party_temp=="Republican"&means.graph$weights=="Unweighted"],
      means.graph$D8[means.graph$year==2021&means.graph$party_temp=="Republican"&means.graph$weights=="Unweighted"],
      means.graph$D1[means.graph$year==2016&means.graph$party_temp=="Democrat"&means.graph$weights=="Unweighted"],
      means.graph$D1[means.graph$year==2021&means.graph$party_temp=="Democrat"&means.graph$weights=="Unweighted"],
      means.graph$D2[means.graph$year==2016&means.graph$party_temp=="Democrat"&means.graph$weights=="Unweighted"],
      means.graph$D2[means.graph$year==2021&means.graph$party_temp=="Democrat"&means.graph$weights=="Unweighted"],
      means.graph$D3[means.graph$year==2016&means.graph$party_temp=="Democrat"&means.graph$weights=="Unweighted"],
      means.graph$D3[means.graph$year==2021&means.graph$party_temp=="Democrat"&means.graph$weights=="Unweighted"],
      means.graph$D4[means.graph$year==2016&means.graph$party_temp=="Democrat"&means.graph$weights=="Unweighted"],
      means.graph$D4[means.graph$year==2021&means.graph$party_temp=="Democrat"&means.graph$weights=="Unweighted"],
      means.graph$D5[means.graph$year==2016&means.graph$party_temp=="Democrat"&means.graph$weights=="Unweighted"],
      means.graph$D5[means.graph$year==2021&means.graph$party_temp=="Democrat"&means.graph$weights=="Unweighted"],
      means.graph$D6[means.graph$year==2016&means.graph$party_temp=="Democrat"&means.graph$weights=="Unweighted"],
      means.graph$D6[means.graph$year==2021&means.graph$party_temp=="Democrat"&means.graph$weights=="Unweighted"],
      means.graph$D7[means.graph$year==2016&means.graph$party_temp=="Democrat"&means.graph$weights=="Unweighted"],
      means.graph$D7[means.graph$year==2021&means.graph$party_temp=="Democrat"&means.graph$weights=="Unweighted"],
      means.graph$D8[means.graph$year==2016&means.graph$party_temp=="Democrat"&means.graph$weights=="Unweighted"],
      means.graph$D8[means.graph$year==2021&means.graph$party_temp=="Democrat"&means.graph$weights=="Unweighted"]
      
)

se=c(
  means.graph$R1.se[means.graph$year==2016&means.graph$party_temp=="Republican"&means.graph$weights=="Unweighted"],
  means.graph$R1.se[means.graph$year==2021&means.graph$party_temp=="Republican"&means.graph$weights=="Unweighted"],
  means.graph$R2.se[means.graph$year==2016&means.graph$party_temp=="Republican"&means.graph$weights=="Unweighted"],
  means.graph$R2.se[means.graph$year==2021&means.graph$party_temp=="Republican"&means.graph$weights=="Unweighted"],
  means.graph$R3.se[means.graph$year==2016&means.graph$party_temp=="Republican"&means.graph$weights=="Unweighted"],
  means.graph$R3.se[means.graph$year==2021&means.graph$party_temp=="Republican"&means.graph$weights=="Unweighted"],
  means.graph$R4.se[means.graph$year==2016&means.graph$party_temp=="Republican"&means.graph$weights=="Unweighted"],
  means.graph$R4.se[means.graph$year==2021&means.graph$party_temp=="Republican"&means.graph$weights=="Unweighted"],
  means.graph$R5.se[means.graph$year==2016&means.graph$party_temp=="Republican"&means.graph$weights=="Unweighted"],
  means.graph$R5.se[means.graph$year==2021&means.graph$party_temp=="Republican"&means.graph$weights=="Unweighted"],
  means.graph$R6.se[means.graph$year==2016&means.graph$party_temp=="Republican"&means.graph$weights=="Unweighted"],
  means.graph$R6.se[means.graph$year==2021&means.graph$party_temp=="Republican"&means.graph$weights=="Unweighted"],
  means.graph$R7.se[means.graph$year==2016&means.graph$party_temp=="Republican"&means.graph$weights=="Unweighted"],
  means.graph$R7.se[means.graph$year==2021&means.graph$party_temp=="Republican"&means.graph$weights=="Unweighted"],
  means.graph$R8.se[means.graph$year==2016&means.graph$party_temp=="Republican"&means.graph$weights=="Unweighted"],
  means.graph$R8.se[means.graph$year==2021&means.graph$party_temp=="Republican"&means.graph$weights=="Unweighted"],
  means.graph$R1.se[means.graph$year==2016&means.graph$party_temp=="Democrat"&means.graph$weights=="Unweighted"],
  means.graph$R1.se[means.graph$year==2021&means.graph$party_temp=="Democrat"&means.graph$weights=="Unweighted"],
  means.graph$R2.se[means.graph$year==2016&means.graph$party_temp=="Democrat"&means.graph$weights=="Unweighted"],
  means.graph$R2.se[means.graph$year==2021&means.graph$party_temp=="Democrat"&means.graph$weights=="Unweighted"],
  means.graph$R3.se[means.graph$year==2016&means.graph$party_temp=="Democrat"&means.graph$weights=="Unweighted"],
  means.graph$R3.se[means.graph$year==2021&means.graph$party_temp=="Democrat"&means.graph$weights=="Unweighted"],
  means.graph$R4.se[means.graph$year==2016&means.graph$party_temp=="Democrat"&means.graph$weights=="Unweighted"],
  means.graph$R4.se[means.graph$year==2021&means.graph$party_temp=="Democrat"&means.graph$weights=="Unweighted"],
  means.graph$R5.se[means.graph$year==2016&means.graph$party_temp=="Democrat"&means.graph$weights=="Unweighted"],
  means.graph$R5.se[means.graph$year==2021&means.graph$party_temp=="Democrat"&means.graph$weights=="Unweighted"],
  means.graph$R6.se[means.graph$year==2016&means.graph$party_temp=="Democrat"&means.graph$weights=="Unweighted"],
  means.graph$R6.se[means.graph$year==2021&means.graph$party_temp=="Democrat"&means.graph$weights=="Unweighted"],
  means.graph$R7.se[means.graph$year==2016&means.graph$party_temp=="Democrat"&means.graph$weights=="Unweighted"],
  means.graph$R7.se[means.graph$year==2021&means.graph$party_temp=="Democrat"&means.graph$weights=="Unweighted"],
  means.graph$R8.se[means.graph$year==2016&means.graph$party_temp=="Democrat"&means.graph$weights=="Unweighted"],
  means.graph$R8.se[means.graph$year==2021&means.graph$party_temp=="Democrat"&means.graph$weights=="Unweighted"],
  means.graph$D1.se[means.graph$year==2016&means.graph$party_temp=="Republican"&means.graph$weights=="Unweighted"],
  means.graph$D1.se[means.graph$year==2021&means.graph$party_temp=="Republican"&means.graph$weights=="Unweighted"],
  means.graph$D2.se[means.graph$year==2016&means.graph$party_temp=="Republican"&means.graph$weights=="Unweighted"],
  means.graph$D2.se[means.graph$year==2021&means.graph$party_temp=="Republican"&means.graph$weights=="Unweighted"],
  means.graph$D3.se[means.graph$year==2016&means.graph$party_temp=="Republican"&means.graph$weights=="Unweighted"],
  means.graph$D3.se[means.graph$year==2021&means.graph$party_temp=="Republican"&means.graph$weights=="Unweighted"],
  means.graph$D4.se[means.graph$year==2016&means.graph$party_temp=="Republican"&means.graph$weights=="Unweighted"],
  means.graph$D4.se[means.graph$year==2021&means.graph$party_temp=="Republican"&means.graph$weights=="Unweighted"],
  means.graph$D5.se[means.graph$year==2016&means.graph$party_temp=="Republican"&means.graph$weights=="Unweighted"],
  means.graph$D5.se[means.graph$year==2021&means.graph$party_temp=="Republican"&means.graph$weights=="Unweighted"],
  means.graph$D6.se[means.graph$year==2016&means.graph$party_temp=="Republican"&means.graph$weights=="Unweighted"],
  means.graph$D6.se[means.graph$year==2021&means.graph$party_temp=="Republican"&means.graph$weights=="Unweighted"],
  means.graph$D7.se[means.graph$year==2016&means.graph$party_temp=="Republican"&means.graph$weights=="Unweighted"],
  means.graph$D7.se[means.graph$year==2021&means.graph$party_temp=="Republican"&means.graph$weights=="Unweighted"],
  means.graph$D8.se[means.graph$year==2016&means.graph$party_temp=="Republican"&means.graph$weights=="Unweighted"],
  means.graph$D8.se[means.graph$year==2021&means.graph$party_temp=="Republican"&means.graph$weights=="Unweighted"],
  means.graph$D1.se[means.graph$year==2016&means.graph$party_temp=="Democrat"&means.graph$weights=="Unweighted"],
  means.graph$D1.se[means.graph$year==2021&means.graph$party_temp=="Democrat"&means.graph$weights=="Unweighted"],
  means.graph$D2.se[means.graph$year==2016&means.graph$party_temp=="Democrat"&means.graph$weights=="Unweighted"],
  means.graph$D2.se[means.graph$year==2021&means.graph$party_temp=="Democrat"&means.graph$weights=="Unweighted"],
  means.graph$D3.se[means.graph$year==2016&means.graph$party_temp=="Democrat"&means.graph$weights=="Unweighted"],
  means.graph$D3.se[means.graph$year==2021&means.graph$party_temp=="Democrat"&means.graph$weights=="Unweighted"],
  means.graph$D4.se[means.graph$year==2016&means.graph$party_temp=="Democrat"&means.graph$weights=="Unweighted"],
  means.graph$D4.se[means.graph$year==2021&means.graph$party_temp=="Democrat"&means.graph$weights=="Unweighted"],
  means.graph$D5.se[means.graph$year==2016&means.graph$party_temp=="Democrat"&means.graph$weights=="Unweighted"],
  means.graph$D5.se[means.graph$year==2021&means.graph$party_temp=="Democrat"&means.graph$weights=="Unweighted"],
  means.graph$D6.se[means.graph$year==2016&means.graph$party_temp=="Democrat"&means.graph$weights=="Unweighted"],
  means.graph$D6.se[means.graph$year==2021&means.graph$party_temp=="Democrat"&means.graph$weights=="Unweighted"],
  means.graph$D7.se[means.graph$year==2016&means.graph$party_temp=="Democrat"&means.graph$weights=="Unweighted"],
  means.graph$D7.se[means.graph$year==2021&means.graph$party_temp=="Democrat"&means.graph$weights=="Unweighted"],
  means.graph$D8.se[means.graph$year==2016&means.graph$party_temp=="Democrat"&means.graph$weights=="Unweighted"],
  means.graph$D8.se[means.graph$year==2021&means.graph$party_temp=="Democrat"&means.graph$weights=="Unweighted"]
)

year=c(rep(c(2016,2021),32)
)

#Add these together as one data object
t2=as.data.frame(cbind(party, respondent))
t2$topic=topic
t2$bar=bar
t2$year=year
t2$year=as.factor(year)
t2$se=se
t2$low=t2$bar-(1.96*t2$se)
t2$high=t2$bar+(1.96*t2$se)

#Now, how to graph it
bar2w=ggplot(data=t2[t2$party=="Stereotypes of Democrats"&t2$respondent=="Democratic respondents",], aes(x=topic, y=bar, fill=year, group=year))+
  geom_bar(position=position_dodge(), stat="identity")+
  geom_errorbar(aes(ymin=low, ymax=high),width=0.2, position=position_dodge((0.9)))+
  xlab("")+
  ylab("Democratic respondents")+
  ggtitle("")+
  theme(plot.caption=element_text(hjust=0))+
  scale_fill_grey()+
  scale_x_continuous(breaks = unique(t$topic),
                     labels=c("1. Bad People", "2. Nonresponse", "3. Liberal Coalition",
                              "4. Class Interests", "5. Dishonest and Lazy", "6. Smart and Honest",
                              "7. Insincere Positive", "8. Caring and Inclusive"))+
  scale_y_continuous(limits=c(-0.06,0.7))
bar2w=bar2w+coord_flip()+theme_bw()+theme(legend.position = "none")

bar2.1w=ggplot(data=t2[t2$party=="Stereotypes of Democrats"&t2$respondent=="Republican respondents",], aes(x=topic, y=bar, fill=year, group=year))+
  geom_bar(position=position_dodge(), stat="identity")+
  geom_errorbar(aes(ymin=low, ymax=high),width=0.2, position=position_dodge((0.9)))+
  xlab("")+
  ylab("Republican respondents")+
  ggtitle("")+
  theme(plot.caption=element_text(hjust=0))+
  scale_fill_grey()+
  scale_x_continuous(breaks = unique(t$topic),
                     labels=c("", "", "",
                              "", "", "",
                              "", ""))+
  scale_y_continuous(limits=c(-0.06,0.7))
bar2.1w=bar2.1w+coord_flip()+theme_bw()+
  theme(legend.position = "none")

bar2.2w=ggplot(data=t2[t2$party=="Stereotypes of Republicans"&t2$respondent=="Democratic respondents",], aes(x=topic, y=bar, fill=year, group=year))+
  geom_bar(position=position_dodge(), stat="identity")+
  geom_errorbar(aes(ymin=low, ymax=high),width=0.2, position=position_dodge((0.9)))+
  xlab("")+
  ylab("Democratic respondents")+
  ggtitle("")+
  theme(plot.caption=element_text(hjust=0))+
  scale_fill_grey()+
  scale_x_continuous(breaks = unique(t$topic),
                     labels=c("1. Nonresponse", "2. Caring Patriots", "3. Rich White Men",
                              "4. Selfish Bigots", "5. Traditional Coalition", "6. Class Interests",
                              "7. Religious \nFundamentalists", "8. Insincere Positive"))+
  scale_y_continuous(limits=c(-0.06,0.7))
bar2.2w=bar2.2w+coord_flip()+theme_bw()+
  theme(legend.position = "bottom",
        legend.key.size=unit(0.3, "cm"),
        legend.text=element_text(size=8),
        legend.margin=margin(0))+ theme(legend.title=element_blank())

bar2.3w=ggplot(data=t2[t2$party=="Stereotypes of Republicans"&t2$respondent=="Republican respondents",], aes(x=topic, y=bar, fill=year, group=year))+
  geom_bar(position=position_dodge(), stat="identity")+
  geom_errorbar(aes(ymin=low, ymax=high),width=0.2, position=position_dodge((0.9)))+
  xlab("")+
  ylab("Republican respondents")+
  ggtitle("")+
  labs(caption="Figure presents topic proportions along with 95 percent confidence intervals")+
  theme(plot.caption=element_text(hjust=0))+
  scale_fill_grey()+
  scale_x_continuous(breaks = unique(t$topic),
                     labels=c("", "", "",
                              "", "", "",
                              "", ""))+
  scale_y_continuous(limits=c(-0.06,0.7))
bar2.3w=bar2.3w+coord_flip()+theme_bw()+
  theme(legend.position = "none")

plot_grid(bar2w, bar2.1w, bar2.2w, bar2.3w, labels=c("Stereotypes of Democrats", "", "Stereotypes of Republicans", ""),
          rel_widths = c(1.1,0.9, 1.5, 1), rel_heights = c(1,1,1,0.85))

jpeg("Figure B.2-unweighted-mean-final.jpeg", width=10, height=8, units="in", res=600)
plot_grid(bar2w, bar2.1w, bar2.2w, bar2.3w, labels=c("Stereotypes of Democrats", "", "Stereotypes of Republicans", ""),
          rel_widths = c(1.1,0.9, 1.5, 1), rel_heights = c(1,1,1,0.85))
dev.off()

#Figure B.3: Weighted regressions:
#Weighted regressions for all of the topics
##Democratic respondents
dTopic1_dem_w=lm_robust(Topic1~inc + gender + white + interest + education2 + 
                          hisp2 + age + pk_scale + year2021, data=mergedD[mergedD$dem==1,], weights=weight)

dTopic2_dem_w=lm_robust(Topic2~inc + gender + white + interest + education2 + 
                          hisp2 + age + pk_scale + year2021, data=mergedD[mergedD$dem==1,], weights=weight)

dTopic3_dem_w=lm_robust(Topic3~inc + gender + white + interest + education2 + 
                          hisp2 + age + pk_scale + year2021, data=mergedD[mergedD$dem==1,], weights=weight)

dTopic4_dem_w=lm_robust(Topic4~inc + gender + white + interest + education2 + 
                          hisp2 + age + pk_scale + year2021, data=mergedD[mergedD$dem==1,], weights=weight)

dTopic5_dem_w=lm_robust(Topic5~inc + gender + white + interest + education2 + 
                          hisp2 + age + pk_scale + year2021, data=mergedD[mergedD$dem==1,], weights=weight)

dTopic6_dem_w=lm_robust(Topic6~inc + gender + white + interest + education2 + 
                          hisp2 + age + pk_scale + year2021, data=mergedD[mergedD$dem==1,], weights=weight)

dTopic7_dem_w=lm_robust(Topic7~inc + gender + white + interest + education2 + 
                          hisp2 + age + pk_scale + year2021, data=mergedD[mergedD$dem==1,], weights=weight)

dTopic8_dem_w=lm_robust(Topic8~inc + gender + white + interest + education2 + 
                          hisp2 + age + pk_scale + year2021, data=mergedD[mergedD$dem==1,], weights=weight)

#Republican topics
rTopic1_dem_w=lm_robust(Topic1~inc + gender + white + interest + education2 + 
                          hisp2 + age + pk_scale + year2021, data=mergedR[mergedR$dem==1,], weights=weight)

rTopic2_dem_w=lm_robust(Topic2~inc + gender + white + interest + education2 + 
                          hisp2 + age + pk_scale + year2021, data=mergedR[mergedR$dem==1,], weights=weight)

rTopic3_dem_w=lm_robust(Topic3~inc + gender + white + interest + education2 + 
                          hisp2 + age + pk_scale + year2021, data=mergedR[mergedR$dem==1,], weights=weight)

rTopic4_dem_w=lm_robust(Topic4~inc + gender + white + interest + education2 + 
                          hisp2 + age + pk_scale + year2021, data=mergedR[mergedR$dem==1,], weights=weight)

rTopic5_dem_w=lm_robust(Topic5~inc + gender + white + interest + education2 + 
                          hisp2 + age + pk_scale + year2021, data=mergedR[mergedR$dem==1,], weights=weight)

rTopic6_dem_w=lm_robust(Topic6~inc + gender + white + interest + education2 + 
                          hisp2 + age + pk_scale + year2021, data=mergedR[mergedR$dem==1,], weights=weight)

rTopic7_dem_w=lm_robust(Topic7~inc + gender + white + interest + education2 + 
                          hisp2 + age + pk_scale + year2021, data=mergedR[mergedR$dem==1,], weights=weight)

rTopic8_dem_w=lm_robust(Topic8~inc + gender + white + interest + education2 + 
                          hisp2 + age + pk_scale + year2021, data=mergedR[mergedR$dem==1,], weights=weight)
##Republican respondents:
dTopic1_rep_w=lm_robust(Topic1~inc + gender + white + interest + education2 + 
                          hisp2 + age + pk_scale + year2021, data=mergedD[mergedD$rep==1,], weights=weight)

dTopic2_rep_w=lm_robust(Topic2~inc + gender + white + interest + education2 + 
                          hisp2 + age + pk_scale + year2021, data=mergedD[mergedD$rep==1,], weights=weight)

dTopic3_rep_w=lm_robust(Topic3~inc + gender + white + interest + education2 + 
                          hisp2 + age + pk_scale + year2021, data=mergedD[mergedD$rep==1,], weights=weight)

dTopic4_rep_w=lm_robust(Topic4~inc + gender + white + interest + education2 + 
                          hisp2 + age + pk_scale + year2021, data=mergedD[mergedD$rep==1,], weights=weight)

dTopic5_rep_w=lm_robust(Topic5~inc + gender + white + interest + education2 + 
                          hisp2 + age + pk_scale + year2021, data=mergedD[mergedD$rep==1,], weights=weight)

dTopic6_rep_w=lm_robust(Topic6~inc + gender + white + interest + education2 + 
                          hisp2 + age + pk_scale + year2021, data=mergedD[mergedD$rep==1,], weights=weight)

dTopic7_rep_w=lm_robust(Topic7~inc + gender + white + interest + education2 + 
                          hisp2 + age + pk_scale + year2021, data=mergedD[mergedD$rep==1,], weights=weight)

dTopic8_rep_w=lm_robust(Topic8~inc + gender + white + interest + education2 + 
                          hisp2 + age + pk_scale + year2021, data=mergedD[mergedD$rep==1,], weights=weight)

#Republican topics
rTopic1_rep_w=lm_robust(Topic1~inc + gender + white + interest + education2 + 
                          hisp2 + age + pk_scale + year2021, data=mergedR[mergedR$rep==1,], weights=weight)

rTopic2_rep_w=lm_robust(Topic2~inc + gender + white + interest + education2 + 
                          hisp2 + age + pk_scale + year2021, data=mergedR[mergedR$rep==1,], weights=weight)

rTopic3_rep_w=lm_robust(Topic3~inc + gender + white + interest + education2 + 
                          hisp2 + age + pk_scale + year2021, data=mergedR[mergedR$rep==1,], weights=weight)

rTopic4_rep_w=lm_robust(Topic4~inc + gender + white + interest + education2 + 
                          hisp2 + age + pk_scale + year2021, data=mergedR[mergedR$rep==1,], weights=weight)

rTopic5_rep_w=lm_robust(Topic5~inc + gender + white + interest + education2 + 
                          hisp2 + age + pk_scale + year2021, data=mergedR[mergedR$rep==1,], weights=weight)

rTopic6_rep_w=lm_robust(Topic6~inc + gender + white + interest + education2 + 
                          hisp2 + age + pk_scale + year2021, data=mergedR[mergedR$rep==1,], weights=weight)

rTopic7_rep_w=lm_robust(Topic7~inc + gender + white + interest + education2 + 
                          hisp2 + age + pk_scale + year2021, data=mergedR[mergedR$rep==1,], weights=weight)

rTopic8_rep_w=lm_robust(Topic8~inc + gender + white + interest + education2 + 
                          hisp2 + age + pk_scale + year2021, data=mergedR[mergedR$rep==1,], weights=weight)

#To make the figure, we need to make a dataframe
year=c(2016,2021)

dem_predict=data.frame(year)
dem_predict$inc[dem_predict$year==2021]=mean(mergedD$inc[mergedD$dem==1&mergedD$year2021==1], na.rm=T)
dem_predict$inc[dem_predict$year==2016]=mean(mergedD$inc[mergedD$dem==1&mergedD$year2021==0], na.rm=T)
dem_predict$gender[dem_predict$year==2021]=mean(mergedD$gender[mergedD$dem==1&mergedD$year2021==1], na.rm=T)
dem_predict$gender[dem_predict$year==2016]=mean(mergedD$gender[mergedD$dem==1&mergedD$year2021==0], na.rm=T)
dem_predict$white[dem_predict$year==2021]=mean(mergedD$white[mergedD$dem==1&mergedD$year2021==1], na.rm=T)
dem_predict$white[dem_predict$year==2016]=mean(mergedD$white[mergedD$dem==1&mergedD$year2021==0], na.rm=T)
dem_predict$interest[dem_predict$year==2021]=mean(mergedD$interest[mergedD$dem==1&mergedD$year2021==1], na.rm=T)
dem_predict$interest[dem_predict$year==2016]=mean(mergedD$interest[mergedD$dem==1&mergedD$year2021==0], na.rm=T)
dem_predict$education2[dem_predict$year==2021]=mean(mergedD$education2[mergedD$dem==1&mergedD$year2021==1], na.rm=T)
dem_predict$education2[dem_predict$year==2016]=mean(mergedD$education2[mergedD$dem==1&mergedD$year2021==0], na.rm=T)
dem_predict$hisp2[dem_predict$year==2021]=mean(mergedD$hisp2[mergedD$dem==1&mergedD$year2021==1], na.rm=T)
dem_predict$hisp2[dem_predict$year==2016]=mean(mergedD$hisp2[mergedD$dem==1&mergedD$year2021==0], na.rm=T)
dem_predict$age[dem_predict$year==2021]=mean(mergedD$age[mergedD$dem==1&mergedD$year2021==1], na.rm=T)
dem_predict$age[dem_predict$year==2016]=mean(mergedD$age[mergedD$dem==1&mergedD$year2021==0], na.rm=T)
dem_predict$pk_scale[dem_predict$year==2021]=mean(mergedD$pk_scale[mergedD$dem==1&mergedD$year2021==1], na.rm=T)
dem_predict$pk_scale[dem_predict$year==2016]=mean(mergedD$pk_scale[mergedD$dem==1&mergedD$year2021==0], na.rm=T)
dem_predict$year2021[dem_predict$year==2021]=1
dem_predict$year2021[dem_predict$year==2016]=0

rep_predict=data.frame(year)
rep_predict$inc[rep_predict$year==2021]=mean(mergedR$inc[mergedR$dem==1&mergedR$year2021==1], na.rm=T)
rep_predict$inc[rep_predict$year==2016]=mean(mergedR$inc[mergedR$dem==1&mergedR$year2021==0], na.rm=T)
rep_predict$gender[rep_predict$year==2021]=mean(mergedR$gender[mergedR$dem==1&mergedR$year2021==1], na.rm=T)
rep_predict$gender[rep_predict$year==2016]=mean(mergedR$gender[mergedR$dem==1&mergedR$year2021==0], na.rm=T)
rep_predict$white[rep_predict$year==2021]=mean(mergedR$white[mergedR$dem==1&mergedR$year2021==1], na.rm=T)
rep_predict$white[rep_predict$year==2016]=mean(mergedR$white[mergedR$dem==1&mergedR$year2021==0], na.rm=T)
rep_predict$interest[rep_predict$year==2021]=mean(mergedR$interest[mergedR$dem==1&mergedR$year2021==1], na.rm=T)
rep_predict$interest[rep_predict$year==2016]=mean(mergedR$interest[mergedR$dem==1&mergedR$year2021==0], na.rm=T)
rep_predict$education2[rep_predict$year==2021]=mean(mergedR$education2[mergedR$dem==1&mergedR$year2021==1], na.rm=T)
rep_predict$education2[rep_predict$year==2016]=mean(mergedR$education2[mergedR$dem==1&mergedR$year2021==0], na.rm=T)
rep_predict$hisp2[rep_predict$year==2021]=mean(mergedR$hisp2[mergedR$dem==1&mergedR$year2021==1], na.rm=T)
rep_predict$hisp2[rep_predict$year==2016]=mean(mergedR$hisp2[mergedR$dem==1&mergedR$year2021==0], na.rm=T)
rep_predict$age[rep_predict$year==2021]=mean(mergedR$age[mergedR$dem==1&mergedR$year2021==1], na.rm=T)
rep_predict$age[rep_predict$year==2016]=mean(mergedR$age[mergedR$dem==1&mergedR$year2021==0], na.rm=T)
rep_predict$pk_scale[rep_predict$year==2021]=mean(mergedR$pk_scale[mergedR$dem==1&mergedR$year2021==1], na.rm=T)
rep_predict$pk_scale[rep_predict$year==2016]=mean(mergedR$pk_scale[mergedR$dem==1&mergedR$year2021==0], na.rm=T)
rep_predict$year2021[rep_predict$year==2021]=1
rep_predict$year2021[rep_predict$year==2016]=0

dem_predict$D1=predict(dTopic1_dem_w, newdata=dem_predict, se.fit=T)$fit
dem_predict$D1.se=predict(dTopic1_dem_w, newdata=dem_predict, se.fit=T)$se.fit
dem_predict$D2=predict(dTopic2_dem_w, newdata=dem_predict, se.fit=T)$fit
dem_predict$D2.se=predict(dTopic2_dem_w, newdata=dem_predict, se.fit=T)$se.fit
dem_predict$D3=predict(dTopic3_dem_w, newdata=dem_predict, se.fit=T)$fit
dem_predict$D3.se=predict(dTopic3_dem_w, newdata=dem_predict, se.fit=T)$se.fit
dem_predict$D4=predict(dTopic4_dem_w, newdata=dem_predict, se.fit=T)$fit
dem_predict$D4.se=predict(dTopic4_dem_w, newdata=dem_predict, se.fit=T)$se.fit
dem_predict$D5=predict(dTopic5_dem_w, newdata=dem_predict, se.fit=T)$fit
dem_predict$D5.se=predict(dTopic5_dem_w, newdata=dem_predict, se.fit=T)$se.fit
dem_predict$D6=predict(dTopic6_dem_w, newdata=dem_predict, se.fit=T)$fit
dem_predict$D6.se=predict(dTopic6_dem_w, newdata=dem_predict, se.fit=T)$se.fit
dem_predict$D7=predict(dTopic7_dem_w, newdata=dem_predict, se.fit=T)$fit
dem_predict$D7.se=predict(dTopic7_dem_w, newdata=dem_predict, se.fit=T)$se.fit
dem_predict$D8=predict(dTopic8_dem_w, newdata=dem_predict, se.fit=T)$fit
dem_predict$D8.se=predict(dTopic8_dem_w, newdata=dem_predict, se.fit=T)$se.fit
dem_predict$D1=predict(dTopic1_dem_w, newdata=dem_predict, se.fit=T)$fit
dem_predict$R1.se=predict(rTopic1_dem_w, newdata=dem_predict, se.fit=T)$se.fit
dem_predict$R2=predict(rTopic2_dem_w, newdata=dem_predict, se.fit=T)$fit
dem_predict$R2.se=predict(rTopic2_dem_w, newdata=dem_predict, se.fit=T)$se.fit
dem_predict$R3=predict(rTopic3_dem_w, newdata=dem_predict, se.fit=T)$fit
dem_predict$R3.se=predict(rTopic3_dem_w, newdata=dem_predict, se.fit=T)$se.fit
dem_predict$R4=predict(rTopic4_dem_w, newdata=dem_predict, se.fit=T)$fit
dem_predict$R4.se=predict(rTopic4_dem_w, newdata=dem_predict, se.fit=T)$se.fit
dem_predict$R5=predict(rTopic5_dem_w, newdata=dem_predict, se.fit=T)$fit
dem_predict$R5.se=predict(rTopic5_dem_w, newdata=dem_predict, se.fit=T)$se.fit
dem_predict$R6=predict(rTopic6_dem_w, newdata=dem_predict, se.fit=T)$fit
dem_predict$R6.se=predict(rTopic6_dem_w, newdata=dem_predict, se.fit=T)$se.fit
dem_predict$R7=predict(rTopic7_dem_w, newdata=dem_predict, se.fit=T)$fit
dem_predict$R7.se=predict(rTopic7_dem_w, newdata=dem_predict, se.fit=T)$se.fit
dem_predict$R8=predict(rTopic8_dem_w, newdata=dem_predict, se.fit=T)$fit
dem_predict$R8.se=predict(rTopic8_dem_w, newdata=dem_predict, se.fit=T)$se.fit

rep_predict$D1=predict(dTopic1_rep_w, newdata=rep_predict, se.fit=T)$fit
rep_predict$D1.se=predict(dTopic1_rep_w, newdata=rep_predict, se.fit=T)$se.fit
rep_predict$D2=predict(dTopic2_rep_w, newdata=rep_predict, se.fit=T)$fit
rep_predict$D2.se=predict(dTopic2_rep_w, newdata=rep_predict, se.fit=T)$se.fit
rep_predict$D3=predict(dTopic3_rep_w, newdata=rep_predict, se.fit=T)$fit
rep_predict$D3.se=predict(dTopic3_rep_w, newdata=rep_predict, se.fit=T)$se.fit
rep_predict$D4=predict(dTopic4_rep_w, newdata=rep_predict, se.fit=T)$fit
rep_predict$D4.se=predict(dTopic4_rep_w, newdata=rep_predict, se.fit=T)$se.fit
rep_predict$D5=predict(dTopic5_rep_w, newdata=rep_predict, se.fit=T)$fit
rep_predict$D5.se=predict(dTopic5_rep_w, newdata=rep_predict, se.fit=T)$se.fit
rep_predict$D6=predict(dTopic6_rep_w, newdata=rep_predict, se.fit=T)$fit
rep_predict$D6.se=predict(dTopic6_rep_w, newdata=rep_predict, se.fit=T)$se.fit
rep_predict$D7=predict(dTopic7_rep_w, newdata=rep_predict, se.fit=T)$fit
rep_predict$D7.se=predict(dTopic7_rep_w, newdata=rep_predict, se.fit=T)$se.fit
rep_predict$D8=predict(dTopic8_rep_w, newdata=rep_predict, se.fit=T)$fit
rep_predict$D8.se=predict(dTopic8_rep_w, newdata=rep_predict, se.fit=T)$se.fit
rep_predict$D1=predict(dTopic1_rep_w, newdata=rep_predict, se.fit=T)$fit
rep_predict$R1.se=predict(rTopic1_rep_w, newdata=rep_predict, se.fit=T)$se.fit
rep_predict$R2=predict(rTopic2_rep_w, newdata=rep_predict, se.fit=T)$fit
rep_predict$R2.se=predict(rTopic2_rep_w, newdata=rep_predict, se.fit=T)$se.fit
rep_predict$R3=predict(rTopic3_rep_w, newdata=rep_predict, se.fit=T)$fit
rep_predict$R3.se=predict(rTopic3_rep_w, newdata=rep_predict, se.fit=T)$se.fit
rep_predict$R4=predict(rTopic4_rep_w, newdata=rep_predict, se.fit=T)$fit
rep_predict$R4.se=predict(rTopic4_rep_w, newdata=rep_predict, se.fit=T)$se.fit
rep_predict$R5=predict(rTopic5_rep_w, newdata=rep_predict, se.fit=T)$fit
rep_predict$R5.se=predict(rTopic5_rep_w, newdata=rep_predict, se.fit=T)$se.fit
rep_predict$R6=predict(rTopic6_rep_w, newdata=rep_predict, se.fit=T)$fit
rep_predict$R6.se=predict(rTopic6_rep_w, newdata=rep_predict, se.fit=T)$se.fit
rep_predict$R7=predict(rTopic7_rep_w, newdata=rep_predict, se.fit=T)$fit
rep_predict$R7.se=predict(rTopic7_rep_w, newdata=rep_predict, se.fit=T)$se.fit
rep_predict$R8=predict(rTopic8_rep_w, newdata=rep_predict, se.fit=T)$fit
rep_predict$R8.se=predict(rTopic8_rep_w, newdata=rep_predict, se.fit=T)$se.fit

##Weighted results##
#Make this into a similar structure as Figure 1
party=c(rep(c("Stereotypes of Republicans", "Stereotypes of Republicans"),16),
        rep(c("Stereotypes of Democrats", "Stereotypes of Democrats"),16)
)
respondent=c(rep("Republican respondents",16), 
             rep("Democratic respondents",16),
             rep("Republican respondents",16), 
             rep("Democratic respondents",16))

topic=c(1,1,
        2,2,
        3,3,
        4,4,
        5,5,
        6,6,
        7,7,
        8,8,
        1,1,
        2,2,
        3,3,
        4,4,
        5,5,
        6,6,
        7,7,
        8,8,
        1,1,
        2,2,
        3,3,
        4,4,
        5,5,
        6,6,
        7,7,
        8,8,
        1,1,
        2,2,
        3,3,
        4,4,
        5,5,
        6,6,
        7,7,
        8,8
)

bar=c(
  rep_predict$R1[rep_predict$year=="2016"],
  rep_predict$R1[rep_predict$year=="2021"],
  rep_predict$R2[rep_predict$year=="2016"],
  rep_predict$R2[rep_predict$year=="2021"],
  rep_predict$R3[rep_predict$year=="2016"],
  rep_predict$R3[rep_predict$year=="2021"],
  rep_predict$R4[rep_predict$year=="2016"],
  rep_predict$R4[rep_predict$year=="2021"],
  rep_predict$R5[rep_predict$year=="2016"],
  rep_predict$R5[rep_predict$year=="2021"],
  rep_predict$R6[rep_predict$year=="2016"],
  rep_predict$R6[rep_predict$year=="2021"],
  rep_predict$R7[rep_predict$year=="2016"],
  rep_predict$R7[rep_predict$year=="2021"],
  rep_predict$R8[rep_predict$year=="2016"],
  rep_predict$R8[rep_predict$year=="2021"],
  dem_predict$R1[dem_predict$year=="2016"],
  dem_predict$R1[dem_predict$year=="2021"],
  dem_predict$R2[dem_predict$year=="2016"],
  dem_predict$R2[dem_predict$year=="2021"],
  dem_predict$R3[dem_predict$year=="2016"],
  dem_predict$R3[dem_predict$year=="2021"],
  dem_predict$R4[dem_predict$year=="2016"],
  dem_predict$R4[dem_predict$year=="2021"],
  dem_predict$R5[dem_predict$year=="2016"],
  dem_predict$R5[dem_predict$year=="2021"],
  dem_predict$R6[dem_predict$year=="2016"],
  dem_predict$R6[dem_predict$year=="2021"],
  dem_predict$R7[dem_predict$year=="2016"],
  dem_predict$R7[dem_predict$year=="2021"],
  dem_predict$R8[dem_predict$year=="2016"],
  dem_predict$R8[dem_predict$year=="2021"],
  rep_predict$D1[rep_predict$year=="2016"],
  rep_predict$D1[rep_predict$year=="2021"],
  rep_predict$D2[rep_predict$year=="2016"],
  rep_predict$D2[rep_predict$year=="2021"],
  rep_predict$D3[rep_predict$year=="2016"],
  rep_predict$D3[rep_predict$year=="2021"],
  rep_predict$D4[rep_predict$year=="2016"],
  rep_predict$D4[rep_predict$year=="2021"],
  rep_predict$D5[rep_predict$year=="2016"],
  rep_predict$D5[rep_predict$year=="2021"],
  rep_predict$D6[rep_predict$year=="2016"],
  rep_predict$D6[rep_predict$year=="2021"],
  rep_predict$D7[rep_predict$year=="2016"],
  rep_predict$D7[rep_predict$year=="2021"],
  rep_predict$D8[rep_predict$year=="2016"],
  rep_predict$D8[rep_predict$year=="2021"],
  dem_predict$D1[dem_predict$year=="2016"],
  dem_predict$D1[dem_predict$year=="2021"],
  dem_predict$D2[dem_predict$year=="2016"],
  dem_predict$D2[dem_predict$year=="2021"],
  dem_predict$D3[dem_predict$year=="2016"],
  dem_predict$D3[dem_predict$year=="2021"],
  dem_predict$D4[dem_predict$year=="2016"],
  dem_predict$D4[dem_predict$year=="2021"],
  dem_predict$D5[dem_predict$year=="2016"],
  dem_predict$D5[dem_predict$year=="2021"],
  dem_predict$D6[dem_predict$year=="2016"],
  dem_predict$D6[dem_predict$year=="2021"],
  dem_predict$D7[dem_predict$year=="2016"],
  dem_predict$D7[dem_predict$year=="2021"],
  dem_predict$D8[dem_predict$year=="2016"],
  dem_predict$D8[dem_predict$year=="2021"]
)

se=c(
  rep_predict$R1.se[rep_predict$year=="2016"],
  rep_predict$R1.se[rep_predict$year=="2021"],
  rep_predict$R2.se[rep_predict$year=="2016"],
  rep_predict$R2.se[rep_predict$year=="2021"],
  rep_predict$R3.se[rep_predict$year=="2016"],
  rep_predict$R3.se[rep_predict$year=="2021"],
  rep_predict$R4.se[rep_predict$year=="2016"],
  rep_predict$R4.se[rep_predict$year=="2021"],
  rep_predict$R5.se[rep_predict$year=="2016"],
  rep_predict$R5.se[rep_predict$year=="2021"],
  rep_predict$R6.se[rep_predict$year=="2016"],
  rep_predict$R6.se[rep_predict$year=="2021"],
  rep_predict$R7.se[rep_predict$year=="2016"],
  rep_predict$R7.se[rep_predict$year=="2021"],
  rep_predict$R8.se[rep_predict$year=="2016"],
  rep_predict$R8.se[rep_predict$year=="2021"],
  dem_predict$R1.se[dem_predict$year=="2016"],
  dem_predict$R1.se[dem_predict$year=="2021"],
  dem_predict$R2.se[dem_predict$year=="2016"],
  dem_predict$R2.se[dem_predict$year=="2021"],
  dem_predict$R3.se[dem_predict$year=="2016"],
  dem_predict$R3.se[dem_predict$year=="2021"],
  dem_predict$R4.se[dem_predict$year=="2016"],
  dem_predict$R4.se[dem_predict$year=="2021"],
  dem_predict$R5.se[dem_predict$year=="2016"],
  dem_predict$R5.se[dem_predict$year=="2021"],
  dem_predict$R6.se[dem_predict$year=="2016"],
  dem_predict$R6.se[dem_predict$year=="2021"],
  dem_predict$R7.se[dem_predict$year=="2016"],
  dem_predict$R7.se[dem_predict$year=="2021"],
  dem_predict$R8.se[dem_predict$year=="2016"],
  dem_predict$R8.se[dem_predict$year=="2021"],
  rep_predict$D1.se[rep_predict$year=="2016"],
  rep_predict$D1.se[rep_predict$year=="2021"],
  rep_predict$D2.se[rep_predict$year=="2016"],
  rep_predict$D2.se[rep_predict$year=="2021"],
  rep_predict$D3.se[rep_predict$year=="2016"],
  rep_predict$D3.se[rep_predict$year=="2021"],
  rep_predict$D4.se[rep_predict$year=="2016"],
  rep_predict$D4.se[rep_predict$year=="2021"],
  rep_predict$D5.se[rep_predict$year=="2016"],
  rep_predict$D5.se[rep_predict$year=="2021"],
  rep_predict$D6.se[rep_predict$year=="2016"],
  rep_predict$D6.se[rep_predict$year=="2021"],
  rep_predict$D7.se[rep_predict$year=="2016"],
  rep_predict$D7.se[rep_predict$year=="2021"],
  rep_predict$D8.se[rep_predict$year=="2016"],
  rep_predict$D8.se[rep_predict$year=="2021"],
  dem_predict$D1.se[dem_predict$year=="2016"],
  dem_predict$D1.se[dem_predict$year=="2021"],
  dem_predict$D2.se[dem_predict$year=="2016"],
  dem_predict$D2.se[dem_predict$year=="2021"],
  dem_predict$D3.se[dem_predict$year=="2016"],
  dem_predict$D3.se[dem_predict$year=="2021"],
  dem_predict$D4.se[dem_predict$year=="2016"],
  dem_predict$D4.se[dem_predict$year=="2021"],
  dem_predict$D5.se[dem_predict$year=="2016"],
  dem_predict$D5.se[dem_predict$year=="2021"],
  dem_predict$D6.se[dem_predict$year=="2016"],
  dem_predict$D6.se[dem_predict$year=="2021"],
  dem_predict$D7.se[dem_predict$year=="2016"],
  dem_predict$D7.se[dem_predict$year=="2021"],
  dem_predict$D8.se[dem_predict$year=="2016"],
  dem_predict$D8.se[dem_predict$year=="2021"]
)

year=c(rep(c(2016,2021),32)
)

#Add these together as one data object
t1=as.data.frame(cbind(party, respondent))
t1$topic=topic
t1$bar=bar
t1$year=year
t1$year=as.factor(year)
t1$se=se
t1$low=t1$bar-(1.96*t1$se)
t1$high=t1$bar+(1.96*t1$se)

#Now, how to graph it
bar2w=ggplot(data=t1[t1$party=="Stereotypes of Democrats"&t1$respondent=="Democratic respondents",], aes(x=topic, y=bar, fill=year, group=year))+
  geom_bar(position=position_dodge(), stat="identity")+
  geom_errorbar(aes(ymin=low, ymax=high),width=0.2, position=position_dodge((0.9)))+
  xlab("")+
  ylab("Democratic respondents")+
  ggtitle("")+
  theme(plot.caption=element_text(hjust=0))+
  scale_fill_grey()+
  scale_x_continuous(breaks = unique(t$topic),
                     labels=c("1. Bad People", "2. Nonresponse", "3. Liberal Coalition",
                              "4. Class Interests", "5. Dishonest and Lazy", "6. Smart and Honest",
                              "7. Insincere Positive", "8. Caring and Inclusive"))+
  scale_y_continuous(limits=c(-0.06,0.7))
bar2w=bar2w+coord_flip()+theme_bw()+theme(legend.position = "none")

bar2.1w=ggplot(data=t1[t1$party=="Stereotypes of Democrats"&t1$respondent=="Republican respondents",], aes(x=topic, y=bar, fill=year, group=year))+
  geom_bar(position=position_dodge(), stat="identity")+
  geom_errorbar(aes(ymin=low, ymax=high),width=0.2, position=position_dodge((0.9)))+
  xlab("")+
  ylab("Republican respondents")+
  ggtitle("")+
  theme(plot.caption=element_text(hjust=0))+
  scale_fill_grey()+
  scale_x_continuous(breaks = unique(t$topic),
                     labels=c("", "", "",
                              "", "", "",
                              "", ""))+
  scale_y_continuous(limits=c(-0.06,0.7))
bar2.1w=bar2.1w+coord_flip()+theme_bw()+
  theme(legend.position = "none")

bar2.2w=ggplot(data=t1[t1$party=="Stereotypes of Republicans"&t1$respondent=="Democratic respondents",], aes(x=topic, y=bar, fill=year, group=year))+
  geom_bar(position=position_dodge(), stat="identity")+
  geom_errorbar(aes(ymin=low, ymax=high),width=0.2, position=position_dodge((0.9)))+
  xlab("")+
  ylab("Democratic respondents")+
  ggtitle("")+
  theme(plot.caption=element_text(hjust=0))+
  scale_fill_grey()+
  scale_x_continuous(breaks = unique(t$topic),
                     labels=c("1. Nonresponse", "2. Caring Patriots", "3. Rich White Men",
                              "4. Selfish Bigots", "5. Traditional Coalition", "6. Class Interests",
                              "7. Religious \nFundamentalists", "8. Insincere Positive"))+
  scale_y_continuous(limits=c(-0.06,0.7))
bar2.2w=bar2.2w+coord_flip()+theme_bw()+
  theme(legend.position = "bottom",
        legend.key.size=unit(0.3, "cm"),
        legend.text=element_text(size=8),
        legend.margin=margin(0))+ theme(legend.title=element_blank())

bar2.3w=ggplot(data=t1[t1$party=="Stereotypes of Republicans"&t1$respondent=="Republican respondents",], aes(x=topic, y=bar, fill=year, group=year))+
  geom_bar(position=position_dodge(), stat="identity")+
  geom_errorbar(aes(ymin=low, ymax=high),width=0.2, position=position_dodge((0.9)))+
  xlab("")+
  ylab("Republican respondents")+
  ggtitle("")+
  labs(caption="Figure presents topic proportions along with 95 percent confidence intervals")+
  theme(plot.caption=element_text(hjust=0))+
  scale_fill_grey()+
  scale_x_continuous(breaks = unique(t$topic),
                     labels=c("", "", "",
                              "", "", "",
                              "", ""))+
  scale_y_continuous(limits=c(-0.06,0.7))
bar2.3w=bar2.3w+coord_flip()+theme_bw()+
  theme(legend.position = "none")

plot_grid(bar2w, bar2.1w, bar2.2w, bar2.3w, labels=c("Stereotypes of Democrats", "", "Stereotypes of Republicans", ""),
          rel_widths = c(1.1,0.9, 1.5, 1), rel_heights = c(1,1,1,0.85))

jpeg("Figure B.3-weighted-regression-final.jpeg", width=10, height=8, units="in", res=600)
plot_grid(bar2w, bar2.1w, bar2.2w, bar2.3w, labels=c("Stereotypes of Democrats", "", "Stereotypes of Republicans", ""),
          rel_widths = c(1.1,0.9, 1.5, 1), rel_heights = c(1,1,1,0.85))
dev.off()

####Appendix C:####
##Table C1 of attention check items for both years:##
#First import the attention check variables for both years
att2021=read.csv("2021 Attention check.csv")
att2016=read.csv("2016 Attention check.csv")

#Append them into one object
att=rbind(att2021, att2016)

table(att$fail_att, att$year2021)
prop.table(table(att$fail_att, att$year2021), 2)
chisq.test(table(att$fail_att, att$year2021))
#p-value of 0.061

#Let's do some proportions tests
att$fail_att2=ifelse(att$fail_att==0, 1, 0)
prop.test(table(att$year2021, att$fail_att2))
#p-value of 0.556

#Figures C1 - C4 - these build directly of the STM objects
#These are done outside of the STM package because they are a robustness check
#not included in our initial analysis plans and not added to the STM objects directly.

#This estimates a series of OLS regressions, one for each topic.
dTopic1_dem_pass=lm_robust(Topic1~inc + gender + white + interest + education2 + 
                             hisp2 + age + pk_scale + year2021, data=mergedD[mergedD$dem==1&mergedD$fail_att==0,])
dTopic1_dem_fail1=lm_robust(Topic1~inc + gender + white + interest + education2 + 
                              hisp2 + age + pk_scale + year2021, data=mergedD[mergedD$dem==1&mergedD$fail_att==1,])
dTopic1_dem_fail2=lm_robust(Topic1~inc + gender + white + interest + education2 + 
                              hisp2 + age + pk_scale + year2021, data=mergedD[mergedD$dem==1&mergedD$fail_att==2,])
dTopic1_rep_pass=lm_robust(Topic1~inc + gender + white + interest + education2 + 
                             hisp2 + age + pk_scale + year2021, data=mergedD[mergedD$rep==1&mergedD$fail_att==0,])
dTopic1_rep_fail1=lm_robust(Topic1~inc + gender + white + interest + education2 + 
                              hisp2 + age + pk_scale + year2021, data=mergedD[mergedD$rep==1&mergedD$fail_att==1,])
dTopic1_rep_fail2=lm_robust(Topic1~inc + gender + white + interest + education2 + 
                              hisp2 + age + pk_scale + year2021, data=mergedD[mergedD$rep==1&mergedD$fail_att==2,])

dTopic2_dem_pass=lm_robust(Topic2~inc + gender + white + interest + education2 + 
                             hisp2 + age + pk_scale + year2021, data=mergedD[mergedD$dem==1&mergedD$fail_att==0,])
dTopic2_dem_fail1=lm_robust(Topic2~inc + gender + white + interest + education2 + 
                              hisp2 + age + pk_scale + year2021, data=mergedD[mergedD$dem==1&mergedD$fail_att==1,])
dTopic2_dem_fail2=lm_robust(Topic2~inc + gender + white + interest + education2 + 
                              hisp2 + age + pk_scale + year2021, data=mergedD[mergedD$dem==1&mergedD$fail_att==2,])
dTopic2_rep_pass=lm_robust(Topic2~inc + gender + white + interest + education2 + 
                             hisp2 + age + pk_scale + year2021, data=mergedD[mergedD$rep==1&mergedD$fail_att==0,])
dTopic2_rep_fail1=lm_robust(Topic2~inc + gender + white + interest + education2 + 
                              hisp2 + age + pk_scale + year2021, data=mergedD[mergedD$rep==1&mergedD$fail_att==1,])
dTopic2_rep_fail2=lm_robust(Topic2~inc + gender + white + interest + education2 + 
                              hisp2 + age + pk_scale + year2021, data=mergedD[mergedD$rep==1&mergedD$fail_att==2,])

dTopic3_dem_pass=lm_robust(Topic3~inc + gender + white + interest + education2 + 
                             hisp2 + age + pk_scale + year2021, data=mergedD[mergedD$dem==1&mergedD$fail_att==0,])
dTopic3_dem_fail1=lm_robust(Topic3~inc + gender + white + interest + education2 + 
                              hisp2 + age + pk_scale + year2021, data=mergedD[mergedD$dem==1&mergedD$fail_att==1,])
dTopic3_dem_fail2=lm_robust(Topic3~inc + gender + white + interest + education2 + 
                              hisp2 + age + pk_scale + year2021, data=mergedD[mergedD$dem==1&mergedD$fail_att==2,])
dTopic3_rep_pass=lm_robust(Topic3~inc + gender + white + interest + education2 + 
                             hisp2 + age + pk_scale + year2021, data=mergedD[mergedD$rep==1&mergedD$fail_att==0,])
dTopic3_rep_fail1=lm_robust(Topic3~inc + gender + white + interest + education2 + 
                              hisp2 + age + pk_scale + year2021, data=mergedD[mergedD$rep==1&mergedD$fail_att==1,])
dTopic3_rep_fail2=lm_robust(Topic3~inc + gender + white + interest + education2 + 
                              hisp2 + age + pk_scale + year2021, data=mergedD[mergedD$rep==1&mergedD$fail_att==2,])

dTopic4_dem_pass=lm_robust(Topic4~inc + gender + white + interest + education2 + 
                             hisp2 + age + pk_scale + year2021, data=mergedD[mergedD$dem==1&mergedD$fail_att==0,])
dTopic4_dem_fail1=lm_robust(Topic4~inc + gender + white + interest + education2 + 
                              hisp2 + age + pk_scale + year2021, data=mergedD[mergedD$dem==1&mergedD$fail_att==1,])
dTopic4_dem_fail2=lm_robust(Topic4~inc + gender + white + interest + education2 + 
                              hisp2 + age + pk_scale + year2021, data=mergedD[mergedD$dem==1&mergedD$fail_att==2,])
dTopic4_rep_pass=lm_robust(Topic4~inc + gender + white + interest + education2 + 
                             hisp2 + age + pk_scale + year2021, data=mergedD[mergedD$rep==1&mergedD$fail_att==0,])
dTopic4_rep_fail1=lm_robust(Topic4~inc + gender + white + interest + education2 + 
                              hisp2 + age + pk_scale + year2021, data=mergedD[mergedD$rep==1&mergedD$fail_att==1,])
dTopic4_rep_fail2=lm_robust(Topic4~inc + gender + white + interest + education2 + 
                              hisp2 + age + pk_scale + year2021, data=mergedD[mergedD$rep==1&mergedD$fail_att==2,])

dTopic5_dem_pass=lm_robust(Topic5~inc + gender + white + interest + education2 + 
                             hisp2 + age + pk_scale + year2021, data=mergedD[mergedD$dem==1&mergedD$fail_att==0,])
dTopic5_dem_fail1=lm_robust(Topic5~inc + gender + white + interest + education2 + 
                              hisp2 + age + pk_scale + year2021, data=mergedD[mergedD$dem==1&mergedD$fail_att==1,])
dTopic5_dem_fail2=lm_robust(Topic5~inc + gender + white + interest + education2 + 
                              hisp2 + age + pk_scale + year2021, data=mergedD[mergedD$dem==1&mergedD$fail_att==2,])
dTopic5_rep_pass=lm_robust(Topic5~inc + gender + white + interest + education2 + 
                             hisp2 + age + pk_scale + year2021, data=mergedD[mergedD$rep==1&mergedD$fail_att==0,])
dTopic5_rep_fail1=lm_robust(Topic5~inc + gender + white + interest + education2 + 
                              hisp2 + age + pk_scale + year2021, data=mergedD[mergedD$rep==1&mergedD$fail_att==1,])
dTopic5_rep_fail2=lm_robust(Topic5~inc + gender + white + interest + education2 + 
                              hisp2 + age + pk_scale + year2021, data=mergedD[mergedD$rep==1&mergedD$fail_att==2,])

dTopic6_dem_pass=lm_robust(Topic6~inc + gender + white + interest + education2 + 
                             hisp2 + age + pk_scale + year2021, data=mergedD[mergedD$dem==1&mergedD$fail_att==0,])
dTopic6_dem_fail1=lm_robust(Topic6~inc + gender + white + interest + education2 + 
                              hisp2 + age + pk_scale + year2021, data=mergedD[mergedD$dem==1&mergedD$fail_att==1,])
dTopic6_dem_fail2=lm_robust(Topic6~inc + gender + white + interest + education2 + 
                              hisp2 + age + pk_scale + year2021, data=mergedD[mergedD$dem==1&mergedD$fail_att==2,])
dTopic6_rep_pass=lm_robust(Topic6~inc + gender + white + interest + education2 + 
                             hisp2 + age + pk_scale + year2021, data=mergedD[mergedD$rep==1&mergedD$fail_att==0,])
dTopic6_rep_fail1=lm_robust(Topic6~inc + gender + white + interest + education2 + 
                              hisp2 + age + pk_scale + year2021, data=mergedD[mergedD$rep==1&mergedD$fail_att==1,])
dTopic6_rep_fail2=lm_robust(Topic6~inc + gender + white + interest + education2 + 
                              hisp2 + age + pk_scale + year2021, data=mergedD[mergedD$rep==1&mergedD$fail_att==2,])

dTopic7_dem_pass=lm_robust(Topic7~inc + gender + white + interest + education2 + 
                             hisp2 + age + pk_scale + year2021, data=mergedD[mergedD$dem==1&mergedD$fail_att==0,])
dTopic7_dem_fail1=lm_robust(Topic7~inc + gender + white + interest + education2 + 
                              hisp2 + age + pk_scale + year2021, data=mergedD[mergedD$dem==1&mergedD$fail_att==1,])
dTopic7_dem_fail2=lm_robust(Topic7~inc + gender + white + interest + education2 + 
                              hisp2 + age + pk_scale + year2021, data=mergedD[mergedD$dem==1&mergedD$fail_att==2,])
dTopic7_rep_pass=lm_robust(Topic7~inc + gender + white + interest + education2 + 
                             hisp2 + age + pk_scale + year2021, data=mergedD[mergedD$rep==1&mergedD$fail_att==0,])
dTopic7_rep_fail1=lm_robust(Topic7~inc + gender + white + interest + education2 + 
                              hisp2 + age + pk_scale + year2021, data=mergedD[mergedD$rep==1&mergedD$fail_att==1,])
dTopic7_rep_fail2=lm_robust(Topic7~inc + gender + white + interest + education2 + 
                              hisp2 + age + pk_scale + year2021, data=mergedD[mergedD$rep==1&mergedD$fail_att==2,])

dTopic8_dem_pass=lm_robust(Topic8~inc + gender + white + interest + education2 + 
                             hisp2 + age + pk_scale + year2021, data=mergedD[mergedD$dem==1&mergedD$fail_att==0,])
dTopic8_dem_fail1=lm_robust(Topic8~inc + gender + white + interest + education2 + 
                              hisp2 + age + pk_scale + year2021, data=mergedD[mergedD$dem==1&mergedD$fail_att==1,])
dTopic8_dem_fail2=lm_robust(Topic8~inc + gender + white + interest + education2 + 
                              hisp2 + age + pk_scale + year2021, data=mergedD[mergedD$dem==1&mergedD$fail_att==2,])
dTopic8_rep_pass=lm_robust(Topic8~inc + gender + white + interest + education2 + 
                             hisp2 + age + pk_scale + year2021, data=mergedD[mergedD$rep==1&mergedD$fail_att==0,])
dTopic8_rep_fail1=lm_robust(Topic8~inc + gender + white + interest + education2 + 
                              hisp2 + age + pk_scale + year2021, data=mergedD[mergedD$rep==1&mergedD$fail_att==1,])
dTopic8_rep_fail2=lm_robust(Topic8~inc + gender + white + interest + education2 + 
                              hisp2 + age + pk_scale + year2021, data=mergedD[mergedD$rep==1&mergedD$fail_att==2,])

#Republican topics
rTopic1_dem_pass=lm_robust(Topic1~inc + gender + white + interest + education2 + 
                             hisp2 + age + pk_scale + year2021, data=mergedR[mergedR$dem==1&mergedR$fail_att==0,])
rTopic1_dem_fail1=lm_robust(Topic1~inc + gender + white + interest + education2 + 
                              hisp2 + age + pk_scale + year2021, data=mergedR[mergedR$dem==1&mergedR$fail_att==1,])
rTopic1_dem_fail2=lm_robust(Topic1~inc + gender + white + interest + education2 + 
                              hisp2 + age + pk_scale + year2021, data=mergedR[mergedR$dem==1&mergedR$fail_att==2,])
rTopic1_rep_pass=lm_robust(Topic1~inc + gender + white + interest + education2 + 
                             hisp2 + age + pk_scale + year2021, data=mergedR[mergedR$rep==1&mergedR$fail_att==0,])
rTopic1_rep_fail1=lm_robust(Topic1~inc + gender + white + interest + education2 + 
                              hisp2 + age + pk_scale + year2021, data=mergedR[mergedR$rep==1&mergedR$fail_att==1,])
rTopic1_rep_fail2=lm_robust(Topic1~inc + gender + white + interest + education2 + 
                              hisp2 + age + pk_scale + year2021, data=mergedR[mergedR$rep==1&mergedR$fail_att==2,])

rTopic2_dem_pass=lm_robust(Topic2~inc + gender + white + interest + education2 + 
                             hisp2 + age + pk_scale + year2021, data=mergedR[mergedR$dem==1&mergedR$fail_att==0,])
rTopic2_dem_fail1=lm_robust(Topic2~inc + gender + white + interest + education2 + 
                              hisp2 + age + pk_scale + year2021, data=mergedR[mergedR$dem==1&mergedR$fail_att==1,])
rTopic2_dem_fail2=lm_robust(Topic2~inc + gender + white + interest + education2 + 
                              hisp2 + age + pk_scale + year2021, data=mergedR[mergedR$dem==1&mergedR$fail_att==2,])
rTopic2_rep_pass=lm_robust(Topic2~inc + gender + white + interest + education2 + 
                             hisp2 + age + pk_scale + year2021, data=mergedR[mergedR$rep==1&mergedR$fail_att==0,])
rTopic2_rep_fail1=lm_robust(Topic2~inc + gender + white + interest + education2 + 
                              hisp2 + age + pk_scale + year2021, data=mergedR[mergedR$rep==1&mergedR$fail_att==1,])
rTopic2_rep_fail2=lm_robust(Topic2~inc + gender + white + interest + education2 + 
                              hisp2 + age + pk_scale + year2021, data=mergedR[mergedR$rep==1&mergedR$fail_att==2,])

rTopic3_dem_pass=lm_robust(Topic3~inc + gender + white + interest + education2 + 
                             hisp2 + age + pk_scale + year2021, data=mergedR[mergedR$dem==1&mergedR$fail_att==0,])
rTopic3_dem_fail1=lm_robust(Topic3~inc + gender + white + interest + education2 + 
                              hisp2 + age + pk_scale + year2021, data=mergedR[mergedR$dem==1&mergedR$fail_att==1,])
rTopic3_dem_fail2=lm_robust(Topic3~inc + gender + white + interest + education2 + 
                              hisp2 + age + pk_scale + year2021, data=mergedR[mergedR$dem==1&mergedR$fail_att==2,])
rTopic3_rep_pass=lm_robust(Topic3~inc + gender + white + interest + education2 + 
                             hisp2 + age + pk_scale + year2021, data=mergedR[mergedR$rep==1&mergedR$fail_att==0,])
rTopic3_rep_fail1=lm_robust(Topic3~inc + gender + white + interest + education2 + 
                              hisp2 + age + pk_scale + year2021, data=mergedR[mergedR$rep==1&mergedR$fail_att==1,])
rTopic3_rep_fail2=lm_robust(Topic3~inc + gender + white + interest + education2 + 
                              hisp2 + age + pk_scale + year2021, data=mergedR[mergedR$rep==1&mergedR$fail_att==2,])

rTopic4_dem_pass=lm_robust(Topic4~inc + gender + white + interest + education2 + 
                             hisp2 + age + pk_scale + year2021, data=mergedR[mergedR$dem==1&mergedR$fail_att==0,])
rTopic4_dem_fail1=lm_robust(Topic4~inc + gender + white + interest + education2 + 
                              hisp2 + age + pk_scale + year2021, data=mergedR[mergedR$dem==1&mergedR$fail_att==1,])
rTopic4_dem_fail2=lm_robust(Topic4~inc + gender + white + interest + education2 + 
                              hisp2 + age + pk_scale + year2021, data=mergedR[mergedR$dem==1&mergedR$fail_att==2,])
rTopic4_rep_pass=lm_robust(Topic4~inc + gender + white + interest + education2 + 
                             hisp2 + age + pk_scale + year2021, data=mergedR[mergedR$rep==1&mergedR$fail_att==0,])
rTopic4_rep_fail1=lm_robust(Topic4~inc + gender + white + interest + education2 + 
                              hisp2 + age + pk_scale + year2021, data=mergedR[mergedR$rep==1&mergedR$fail_att==1,])
rTopic4_rep_fail2=lm_robust(Topic4~inc + gender + white + interest + education2 + 
                              hisp2 + age + pk_scale + year2021, data=mergedR[mergedR$rep==1&mergedR$fail_att==2,])

rTopic5_dem_pass=lm_robust(Topic5~inc + gender + white + interest + education2 + 
                             hisp2 + age + pk_scale + year2021, data=mergedR[mergedR$dem==1&mergedR$fail_att==0,])
rTopic5_dem_fail1=lm_robust(Topic5~inc + gender + white + interest + education2 + 
                              hisp2 + age + pk_scale + year2021, data=mergedR[mergedR$dem==1&mergedR$fail_att==1,])
rTopic5_dem_fail2=lm_robust(Topic5~inc + gender + white + interest + education2 + 
                              hisp2 + age + pk_scale + year2021, data=mergedR[mergedR$dem==1&mergedR$fail_att==2,])
rTopic5_rep_pass=lm_robust(Topic5~inc + gender + white + interest + education2 + 
                             hisp2 + age + pk_scale + year2021, data=mergedR[mergedR$rep==1&mergedR$fail_att==0,])
rTopic5_rep_fail1=lm_robust(Topic5~inc + gender + white + interest + education2 + 
                              hisp2 + age + pk_scale + year2021, data=mergedR[mergedR$rep==1&mergedR$fail_att==1,])
rTopic5_rep_fail2=lm_robust(Topic5~inc + gender + white + interest + education2 + 
                              hisp2 + age + pk_scale + year2021, data=mergedR[mergedR$rep==1&mergedR$fail_att==2,])

rTopic6_dem_pass=lm_robust(Topic6~inc + gender + white + interest + education2 + 
                             hisp2 + age + pk_scale + year2021, data=mergedR[mergedR$dem==1&mergedR$fail_att==0,])
rTopic6_dem_fail1=lm_robust(Topic6~inc + gender + white + interest + education2 + 
                              hisp2 + age + pk_scale + year2021, data=mergedR[mergedR$dem==1&mergedR$fail_att==1,])
rTopic6_dem_fail2=lm_robust(Topic6~inc + gender + white + interest + education2 + 
                              hisp2 + age + pk_scale + year2021, data=mergedR[mergedR$dem==1&mergedR$fail_att==2,])
rTopic6_rep_pass=lm_robust(Topic6~inc + gender + white + interest + education2 + 
                             hisp2 + age + pk_scale + year2021, data=mergedR[mergedR$rep==1&mergedR$fail_att==0,])
rTopic6_rep_fail1=lm_robust(Topic6~inc + gender + white + interest + education2 + 
                              hisp2 + age + pk_scale + year2021, data=mergedR[mergedR$rep==1&mergedR$fail_att==1,])
rTopic6_rep_fail2=lm_robust(Topic6~inc + gender + white + interest + education2 + 
                              hisp2 + age + pk_scale + year2021, data=mergedR[mergedR$rep==1&mergedR$fail_att==2,])

rTopic7_dem_pass=lm_robust(Topic7~inc + gender + white + interest + education2 + 
                             hisp2 + age + pk_scale + year2021, data=mergedR[mergedR$dem==1&mergedR$fail_att==0,])
rTopic7_dem_fail1=lm_robust(Topic7~inc + gender + white + interest + education2 + 
                              hisp2 + age + pk_scale + year2021, data=mergedR[mergedR$dem==1&mergedR$fail_att==1,])
rTopic7_dem_fail2=lm_robust(Topic7~inc + gender + white + interest + education2 + 
                              hisp2 + age + pk_scale + year2021, data=mergedR[mergedR$dem==1&mergedR$fail_att==2,])
rTopic7_rep_pass=lm_robust(Topic7~inc + gender + white + interest + education2 + 
                             hisp2 + age + pk_scale + year2021, data=mergedR[mergedR$rep==1&mergedR$fail_att==0,])
rTopic7_rep_fail1=lm_robust(Topic7~inc + gender + white + interest + education2 + 
                              hisp2 + age + pk_scale + year2021, data=mergedR[mergedR$rep==1&mergedR$fail_att==1,])
rTopic7_rep_fail2=lm_robust(Topic7~inc + gender + white + interest + education2 + 
                              hisp2 + age + pk_scale + year2021, data=mergedR[mergedR$rep==1&mergedR$fail_att==2,])

rTopic8_dem_pass=lm_robust(Topic8~inc + gender + white + interest + education2 + 
                             hisp2 + age + pk_scale + year2021, data=mergedR[mergedR$dem==1&mergedR$fail_att==0,])
rTopic8_dem_fail1=lm_robust(Topic8~inc + gender + white + interest + education2 + 
                              hisp2 + age + pk_scale + year2021, data=mergedR[mergedR$dem==1&mergedR$fail_att==1,])
rTopic8_dem_fail2=lm_robust(Topic8~inc + gender + white + interest + education2 + 
                              hisp2 + age + pk_scale + year2021, data=mergedR[mergedR$dem==1&mergedR$fail_att==2,])
rTopic8_rep_pass=lm_robust(Topic8~inc + gender + white + interest + education2 + 
                             hisp2 + age + pk_scale + year2021, data=mergedR[mergedR$rep==1&mergedR$fail_att==0,])
rTopic8_rep_fail1=lm_robust(Topic8~inc + gender + white + interest + education2 + 
                              hisp2 + age + pk_scale + year2021, data=mergedR[mergedR$rep==1&mergedR$fail_att==1,])
rTopic8_rep_fail2=lm_robust(Topic8~inc + gender + white + interest + education2 + 
                              hisp2 + age + pk_scale + year2021, data=mergedR[mergedR$rep==1&mergedR$fail_att==2,])

##Now for the corresponding numbers from STM for comparison
#STM objects
prep_rep=estimateEffect(1:8~rep*year2021, GenRChoice, meta=outR$meta)
prep_dem=estimateEffect(1:8~dem*year2021, GenRChoice, meta=outR$meta)
#Republican topics#
rep_repeffects=get_effects(estimates=prep_rep, variable='year2021',
                           type="difference", cov_val1=1, cov_val2=0 ,
                           moderator="rep", modval = 1)
rep_repeffects$party="Rep"

rep_dem_effects= get_effects(estimates=prep_dem, variable='year2021',
                             type="difference", cov_val1=1, cov_val2=0 ,
                             moderator="dem", modval = 1)
rep_dem_effects$party="Dem"

rep_effects=rbind(rep_repeffects, rep_dem_effects)

#Democrat topics#
prep_rep=estimateEffect(1:8~rep*year2021, GenDChoice, meta=outD$meta)
prep_dem=estimateEffect(1:8~dem*year2021, GenDChoice, meta=outD$meta)

dem_repeffects=get_effects(estimates=prep_rep, variable='year2021',
                           type="difference", cov_val1=1, cov_val2=0 ,
                           moderator="rep", modval = 1)
dem_repeffects$party="Rep"

dem_dem_effects= get_effects(estimates=prep_dem, variable='year2021',
                             type="difference", cov_val1=1, cov_val2=0 ,
                             moderator="dem", modval = 1)
dem_dem_effects$party="Dem"

dem_effects=rbind(dem_repeffects, dem_dem_effects)

##Graphing Democratic topics##
party=c("Democrat", "Democrat", "Democrat", "Democrat",
        "Republican", "Republican", "Republican", "Republican",
        "Democrat", "Democrat", "Democrat", "Democrat",
        "Republican", "Republican", "Republican", "Republican",
        rep("Democrat", 4),
        rep("Republican", 4),
        rep("Democrat", 4),
        rep("Republican", 4),
        rep("Democrat", 4),
        rep("Republican", 4),
        rep("Democrat", 4),
        rep("Republican", 4),
        rep("Democrat", 4),
        rep("Republican", 4),
        rep("Democrat", 4),
        rep("Republican", 4)
)
model=c(rep(c("Passed", "Failed 1x", "Failed 2x", "STM"),16)
)
topic=c(rep(1,8),
        rep(2,8),
        rep(3,8),
        rep(4,8),
        rep(5,8),
        rep(6,8),
        rep(7,8),
        rep(8,8)
)
low=c(dTopic1_dem_pass$conf.low[10],dTopic1_dem_fail1$conf.low[10],dTopic1_dem_fail2$conf.low[10], dem_effects[9,3],
      dTopic1_rep_pass$conf.low[10],dTopic1_rep_fail1$conf.low[10],dTopic1_rep_fail2$conf.low[10], dem_effects[1,3],
      dTopic2_dem_pass$conf.low[10],dTopic2_dem_fail1$conf.low[10],dTopic2_dem_fail2$conf.low[10], dem_effects[10,3],
      dTopic2_rep_pass$conf.low[10],dTopic2_rep_fail1$conf.low[10],dTopic2_rep_fail2$conf.low[10], dem_effects[2,3],
      dTopic3_dem_pass$conf.low[10],dTopic3_dem_fail1$conf.low[10],dTopic3_dem_fail2$conf.low[10], dem_effects[11,3],
      dTopic3_rep_pass$conf.low[10],dTopic3_rep_fail1$conf.low[10],dTopic3_rep_fail2$conf.low[10], dem_effects[3,3],
      dTopic4_dem_pass$conf.low[10],dTopic4_dem_fail1$conf.low[10],dTopic4_dem_fail2$conf.low[10], dem_effects[12,3],
      dTopic4_rep_pass$conf.low[10],dTopic4_rep_fail1$conf.low[10],dTopic4_rep_fail2$conf.low[10], dem_effects[4,3],
      dTopic5_dem_pass$conf.low[10],dTopic5_dem_fail1$conf.low[10],dTopic5_dem_fail2$conf.low[10], dem_effects[13,3],
      dTopic5_rep_pass$conf.low[10],dTopic5_rep_fail1$conf.low[10],dTopic5_rep_fail2$conf.low[10], dem_effects[5,3],
      dTopic6_dem_pass$conf.low[10],dTopic6_dem_fail1$conf.low[10],dTopic6_dem_fail2$conf.low[10], dem_effects[14,3],
      dTopic6_rep_pass$conf.low[10],dTopic6_rep_fail1$conf.low[10],dTopic6_rep_fail2$conf.low[10], dem_effects[6,3],
      dTopic7_dem_pass$conf.low[10],dTopic7_dem_fail1$conf.low[10],dTopic7_dem_fail2$conf.low[10], dem_effects[15,3],
      dTopic7_rep_pass$conf.low[10],dTopic7_rep_fail1$conf.low[10],dTopic7_rep_fail2$conf.low[10], dem_effects[7,3],
      dTopic8_dem_pass$conf.low[10],dTopic8_dem_fail1$conf.low[10],dTopic8_dem_fail2$conf.low[10], dem_effects[16,3],
      dTopic8_rep_pass$conf.low[10],dTopic8_rep_fail1$conf.low[10],dTopic8_rep_fail2$conf.low[10], dem_effects[8,3]
)
low=as.numeric(low)
high=c(dTopic1_dem_pass$conf.high[10],dTopic1_dem_fail1$conf.high[10],dTopic1_dem_fail2$conf.high[10], dem_effects[9,4],
       dTopic1_rep_pass$conf.high[10],dTopic1_rep_fail1$conf.high[10],dTopic1_rep_fail2$conf.high[10], dem_effects[1,4],
       dTopic2_dem_pass$conf.high[10],dTopic2_dem_fail1$conf.high[10],dTopic2_dem_fail2$conf.high[10], dem_effects[10,4],
       dTopic2_rep_pass$conf.high[10],dTopic2_rep_fail1$conf.high[10],dTopic2_rep_fail2$conf.high[10], dem_effects[2,4],
       dTopic3_dem_pass$conf.high[10],dTopic3_dem_fail1$conf.high[10],dTopic3_dem_fail2$conf.high[10], dem_effects[11,4],
       dTopic3_rep_pass$conf.high[10],dTopic3_rep_fail1$conf.high[10],dTopic3_rep_fail2$conf.high[10], dem_effects[3,4],
       dTopic4_dem_pass$conf.high[10],dTopic4_dem_fail1$conf.high[10],dTopic4_dem_fail2$conf.high[10], dem_effects[12,4],
       dTopic4_rep_pass$conf.high[10],dTopic4_rep_fail1$conf.high[10],dTopic4_rep_fail2$conf.high[10], dem_effects[4,4],
       dTopic5_dem_pass$conf.high[10],dTopic5_dem_fail1$conf.high[10],dTopic5_dem_fail2$conf.high[10], dem_effects[13,4],
       dTopic5_rep_pass$conf.high[10],dTopic5_rep_fail1$conf.high[10],dTopic5_rep_fail2$conf.high[10], dem_effects[5,4],
       dTopic6_dem_pass$conf.high[10],dTopic6_dem_fail1$conf.high[10],dTopic6_dem_fail2$conf.high[10], dem_effects[14,4],
       dTopic6_rep_pass$conf.high[10],dTopic6_rep_fail1$conf.high[10],dTopic6_rep_fail2$conf.high[10], dem_effects[6,4],
       dTopic7_dem_pass$conf.high[10],dTopic7_dem_fail1$conf.high[10],dTopic7_dem_fail2$conf.high[10], dem_effects[15,4],
       dTopic7_rep_pass$conf.high[10],dTopic7_rep_fail1$conf.high[10],dTopic7_rep_fail2$conf.high[10], dem_effects[7,4],
       dTopic8_dem_pass$conf.high[10],dTopic8_dem_fail1$conf.high[10],dTopic8_dem_fail2$conf.high[10], dem_effects[16,4],
       dTopic8_rep_pass$conf.high[10],dTopic8_rep_fail1$conf.high[10],dTopic8_rep_fail2$conf.high[10], dem_effects[8,4]
)
high=as.numeric(high)

#Add these together as one data object
t=as.data.frame(cbind(party, model))
t$topic=topic
t$low=low
t$high=high

##Figure C.1##
plot1=ggplot(data = t[t$party=="Democrat",], aes(x=model, ymin=low, ymax=high))+
  geom_linerange(color="black", size=1) +
  geom_hline(yintercept=0, size=1, linetype="dashed", color="gray") +
  theme_classic()+
  theme(plot.title = element_text(hjust = 0.5))+
  labs(y= "Change in proportion of the topic", 
       x="Model", title="Democratic topics, Democratic respondents")

plot1=plot1+facet_wrap(.~topic, ncol=4)
#Saving as high res .jpeg file
jpeg("Figure C1. Democratic topics_Democrats_different levels of attentiveness.jpeg", width=10, height=6, units="in", res=600)
plot1
dev.off()

##Figure C.2##
plot2=ggplot(data = t[t$party=="Republican",], aes(x=model, ymin=low, ymax=high))+
  geom_linerange(color="black", size=1) +
  geom_hline(yintercept=0, size=1, linetype="dashed", color="gray") +
  theme_classic()+
  theme(plot.title = element_text(hjust = 0.5))+
  labs(y= "Change in proportion of the topic", 
       x="Model", title="Democratic topics, Republican respondents")

plot2=plot2+facet_wrap(.~topic, ncol=4)
#Saving as high res .jpeg file
jpeg("Figure C2. Democratic topics_Republicans__different levels of attentiveness.jpeg", width=10, height=6, units="in", res=600)
plot2
dev.off()


##Graphing Republican topics##
party=c("Democrat", "Democrat", "Democrat", "Democrat",
        "Republican", "Republican", "Republican", "Republican",
        "Democrat", "Democrat", "Democrat", "Democrat",
        "Republican", "Republican", "Republican", "Republican",
        rep("Democrat", 4),
        rep("Republican", 4),
        rep("Democrat", 4),
        rep("Republican", 4),
        rep("Democrat", 4),
        rep("Republican", 4),
        rep("Democrat", 4),
        rep("Republican", 4),
        rep("Democrat", 4),
        rep("Republican", 4),
        rep("Democrat", 4),
        rep("Republican", 4)
)
model=c(rep(c("Passed", "Failed 1x", "Failed 2x", "STM"),16)
)
topic=c(rep(1,8),
        rep(2,8),
        rep(3,8),
        rep(4,8),
        rep(5,8),
        rep(6,8),
        rep(7,8),
        rep(8,8)
)
low=c(rTopic1_dem_pass$conf.low[10],rTopic1_dem_fail1$conf.low[10],rTopic1_dem_fail2$conf.low[10], rep_effects[9,3],
      rTopic1_rep_pass$conf.low[10],rTopic1_rep_fail1$conf.low[10],rTopic1_rep_fail2$conf.low[10], rep_effects[1,3],
      rTopic2_dem_pass$conf.low[10],rTopic2_dem_fail1$conf.low[10],rTopic2_dem_fail2$conf.low[10], rep_effects[10,3],
      rTopic2_rep_pass$conf.low[10],rTopic2_rep_fail1$conf.low[10],rTopic2_rep_fail2$conf.low[10], rep_effects[2,3],
      rTopic3_dem_pass$conf.low[10],rTopic3_dem_fail1$conf.low[10],rTopic3_dem_fail2$conf.low[10], rep_effects[11,3],
      rTopic3_rep_pass$conf.low[10],rTopic3_rep_fail1$conf.low[10],rTopic3_rep_fail2$conf.low[10], rep_effects[3,3],
      rTopic4_dem_pass$conf.low[10],rTopic4_dem_fail1$conf.low[10],rTopic4_dem_fail2$conf.low[10], rep_effects[12,3],
      rTopic4_rep_pass$conf.low[10],rTopic4_rep_fail1$conf.low[10],rTopic4_rep_fail2$conf.low[10], rep_effects[4,3],
      rTopic5_dem_pass$conf.low[10],rTopic5_dem_fail1$conf.low[10],rTopic5_dem_fail2$conf.low[10], rep_effects[13,3],
      rTopic5_rep_pass$conf.low[10],rTopic5_rep_fail1$conf.low[10],rTopic5_rep_fail2$conf.low[10], rep_effects[5,3],
      rTopic6_dem_pass$conf.low[10],rTopic6_dem_fail1$conf.low[10],rTopic6_dem_fail2$conf.low[10], rep_effects[14,3],
      rTopic6_rep_pass$conf.low[10],rTopic6_rep_fail1$conf.low[10],rTopic6_rep_fail2$conf.low[10], rep_effects[6,3],
      rTopic7_dem_pass$conf.low[10],rTopic7_dem_fail1$conf.low[10],rTopic7_dem_fail2$conf.low[10], rep_effects[15,3],
      rTopic7_rep_pass$conf.low[10],rTopic7_rep_fail1$conf.low[10],rTopic7_rep_fail2$conf.low[10], rep_effects[7,3],
      rTopic8_dem_pass$conf.low[10],rTopic8_dem_fail1$conf.low[10],rTopic8_dem_fail2$conf.low[10], rep_effects[16,3],
      rTopic8_rep_pass$conf.low[10],rTopic8_rep_fail1$conf.low[10],rTopic8_rep_fail2$conf.low[10], rep_effects[8,3]
)
low=as.numeric(low)
high=c(rTopic1_dem_pass$conf.high[10],rTopic1_dem_fail1$conf.high[10],rTopic1_dem_fail2$conf.high[10], rep_effects[9,4],
       rTopic1_rep_pass$conf.high[10],rTopic1_rep_fail1$conf.high[10],rTopic1_rep_fail2$conf.high[10], rep_effects[1,4],
       rTopic2_dem_pass$conf.high[10],rTopic2_dem_fail1$conf.high[10],rTopic2_dem_fail2$conf.high[10], rep_effects[10,4],
       rTopic2_rep_pass$conf.high[10],rTopic2_rep_fail1$conf.high[10],rTopic2_rep_fail2$conf.high[10], rep_effects[2,4],
       rTopic3_dem_pass$conf.high[10],rTopic3_dem_fail1$conf.high[10],rTopic3_dem_fail2$conf.high[10], rep_effects[11,4],
       rTopic3_rep_pass$conf.high[10],rTopic3_rep_fail1$conf.high[10],rTopic3_rep_fail2$conf.high[10], rep_effects[3,4],
       rTopic4_dem_pass$conf.high[10],rTopic4_dem_fail1$conf.high[10],rTopic4_dem_fail2$conf.high[10], rep_effects[12,4],
       rTopic4_rep_pass$conf.high[10],rTopic4_rep_fail1$conf.high[10],rTopic4_rep_fail2$conf.high[10], rep_effects[4,4],
       rTopic5_dem_pass$conf.high[10],rTopic5_dem_fail1$conf.high[10],rTopic5_dem_fail2$conf.high[10], rep_effects[13,4],
       rTopic5_rep_pass$conf.high[10],rTopic5_rep_fail1$conf.high[10],rTopic5_rep_fail2$conf.high[10], rep_effects[5,4],
       rTopic6_dem_pass$conf.high[10],rTopic6_dem_fail1$conf.high[10],rTopic6_dem_fail2$conf.high[10], rep_effects[14,4],
       rTopic6_rep_pass$conf.high[10],rTopic6_rep_fail1$conf.high[10],rTopic6_rep_fail2$conf.high[10], rep_effects[6,4],
       rTopic7_dem_pass$conf.high[10],rTopic7_dem_fail1$conf.high[10],rTopic7_dem_fail2$conf.high[10], rep_effects[15,4],
       rTopic7_rep_pass$conf.high[10],rTopic7_rep_fail1$conf.high[10],rTopic7_rep_fail2$conf.high[10], rep_effects[7,4],
       rTopic8_dem_pass$conf.high[10],rTopic8_dem_fail1$conf.high[10],rTopic8_dem_fail2$conf.high[10], rep_effects[16,4],
       rTopic8_rep_pass$conf.high[10],rTopic8_rep_fail1$conf.high[10],rTopic8_rep_fail2$conf.high[10], rep_effects[8,4]
)
high=as.numeric(high)

#Add these together as one data object
t2=as.data.frame(cbind(party, model))
t2$topic=topic
t2$low=low
t2$high=high

##Figure C3##
plot3=ggplot(data = t2[t2$party=="Democrat",], aes(x=model, ymin=low, ymax=high))+
  geom_linerange(color="black", size=1) +
  geom_hline(yintercept=0, size=1, linetype="dashed", color="gray") +
  theme_classic()+
  theme(plot.title = element_text(hjust = 0.5))+
  labs(y= "Change in proportion of the topic", 
       x="Model", title="Republican topics, Democratic respondents")

plot3=plot3+facet_wrap(.~topic, ncol=4)
#Saving as high res .jpeg file
jpeg("Figure C3. Republican topics_Democrats__different levels of attentiveness.jpeg", width=10, height=6, units="in", res=600)
plot3
dev.off()

##Figure C4##
plot4=ggplot(data = t2[t2$party=="Republican",], aes(x=model, ymin=low, ymax=high))+
  geom_linerange(color="black", size=1) +
  geom_hline(yintercept=0, size=1, linetype="dashed", color="gray") +
  theme_classic()+
  theme(plot.title = element_text(hjust = 0.5))+
  labs(y= "Change in proportion of the topic", 
       x="Model", title="Republican topics, Republican respondents")

plot4=plot4+facet_wrap(.~topic, ncol=4)
plot4
#Saving as high res .jpeg file
jpeg("Figure C4. Republican topics_Republicans__different levels of attentiveness.jpeg", width=10, height=6, units="in", res=600)
plot4
dev.off()

####Appendix D:####
##Additional prevalence covariates##
#Appendix D presents the results of our other prevalence covariates. Those results come from
#these estimates:
#Estimate the covariate effects based on the STM
prep_rep_all=estimateEffect(1:8~inc + gender + white + interest + education2 + 
                              hisp2 + age + pk_scale , GenRChoice, meta=outR$meta)
prep_dem_all=estimateEffect(1:8~inc + gender + white + interest + education2 + 
                              hisp2 + age + pk_scale , GenDChoice, meta=outD$meta)

#Summarize to fill in Table D1
summary(prep_rep_all)
#Summarize to fill in Table D2
summary(prep_dem_all)

####Appendix E:####
#These figures are alternative ways to visualize the results as shown in the main text
#Figure E1#
prep_rep=estimateEffect(1:8~rep*year2021, GenDChoice, meta=outD$meta)
prep_dem=estimateEffect(1:8~dem*year2021, GenDChoice, meta=outD$meta)

rep_repeffects=get_effects(estimates=prep_rep, variable='year2021',
                           type="difference", cov_val1=1, cov_val2=0 ,
                           moderator="rep", modval = 1)
rep_repeffects$party="Rep"

rep_dem_effects= get_effects(estimates=prep_dem, variable='year2021',
                             type="difference", cov_val1=1, cov_val2=0 ,
                             moderator="dem", modval = 1)
rep_dem_effects$party="Dem"

rep_effects=rbind(rep_repeffects, rep_dem_effects)

#Make the plot
rep_plot=ggplot(data = rep_effects, aes(x=topic, y=difference, ymin=lower, ymax=upper))+
  geom_pointrange(color="black") +
  geom_hline(yintercept=0, size=1, linetype="dashed", color="red") +
  theme_classic()+
  theme(axis.text.x = element_text(size=7),
        axis.text.y = element_text(size=12),
        axis.title.x = element_text(size=12),
        plot.title = element_text(size=17))+
  theme(plot.title = element_text(hjust = 0.5))+
  theme(legend.title = element_blank())+
  theme(legend.position="right")+
  coord_cartesian(ylim = c(-0.2, 0.15))+
  guides(linetype=guide_legend(keywidth = 2, keyheight = 5))+
  labs(title="", 
       y= "Difference in year (2021-2016)", 
       x="Topic number")
rep_plot+facet_grid(.~party)+theme(strip.text.x = element_text(size = 11))+
  theme(strip.background = element_blank())+coord_flip()+ggtitle("Stereotypes of Democrats")

#Save as hi res .jpeg file
jpeg("Figure E1. Stereotypes of Democrats, changes over time.jpeg", width=7, height=6, units="in", res=600)
rep_plot+facet_grid(.~party)+theme(strip.text.x = element_text(size = 11))+
  theme(strip.background = element_blank())+coord_flip()+ggtitle("Stereotypes of Democrats")
dev.off()

#Figure E2. Republican topics#
prep_rep=estimateEffect(1:8~rep*year2021, GenRChoice, meta=outR$meta)
prep_dem=estimateEffect(1:8~dem*year2021, GenRChoice, meta=outR$meta)

rep_repeffects=get_effects(estimates=prep_rep, variable='year2021',
                           type="difference", cov_val1=1, cov_val2=0 ,
                           moderator="rep", modval = 1)
rep_repeffects$party="Rep"

rep_dem_effects= get_effects(estimates=prep_dem, variable='year2021',
                             type="difference", cov_val1=1, cov_val2=0 ,
                             moderator="dem", modval = 1)
rep_dem_effects$party="Dem"

rep_effects=rbind(rep_repeffects, rep_dem_effects)

#Make the plot
rep_plot=ggplot(data = rep_effects, aes(x=topic, y=difference, ymin=lower, ymax=upper))+
  geom_pointrange(color="black") +
  geom_hline(yintercept=0, size=1, linetype="dashed", color="red") +
  theme_classic()+
  theme(axis.text.x = element_text(size=7),
        axis.text.y = element_text(size=12),
        axis.title.x = element_text(size=12),
        plot.title = element_text(size=17))+
  theme(plot.title = element_text(hjust = 0.5))+
  theme(legend.title = element_blank())+
  theme(legend.position="right")+
  coord_cartesian(ylim = c(-0.2, 0.15))+
  guides(linetype=guide_legend(keywidth = 2, keyheight = 5))+
  labs(title="", 
       y= "Difference in year (2021-2016)", 
       x="Topic number")
rep_plot+facet_grid(.~party)+theme(strip.text.x = element_text(size = 11))+
  theme(strip.background = element_blank())+coord_flip()+ggtitle("Stereotypes of Republicans")

#Save as hi res .jpeg file
jpeg("Figure E2. Stereotypes of Republicans, changes over time.jpeg", width=7, height=6, units="in", res=600)
rep_plot+facet_grid(.~party)+theme(strip.text.x = element_text(size = 11))+
  theme(strip.background = element_blank())+coord_flip()+ggtitle("Stereotypes of Republicans")
dev.off()

#####Appendix F:####
##Exemplar documents##
#To see these, make sure you have a large enough plotting space (if you are using RStudio)

#Democrat stereotypes exemplars
thoughtR8.1.1 <- findThoughts(GenDChoice, texts=outD$meta$GenD, n=10, topics=1)$docs[[1]]
plotQuote(thoughtR8.1.1, main="Dem 8 topics, Topic 1")
thoughtR8.1.2 <- findThoughts(GenDChoice, texts=outD$meta$GenD, n=10, topics=2)$docs[[1]]
plotQuote(thoughtR8.1.2, main="Dem 8 topics, Topic 2")
thoughtR8.1.3 <- findThoughts(GenDChoice, texts=outD$meta$GenD, n=10, topics=3)$docs[[1]]
plotQuote(thoughtR8.1.3, main="Dem 8 topics, Topic 3")
thoughtR8.1.4 <- findThoughts(GenDChoice, texts=outD$meta$GenD, n=10, topics=4)$docs[[1]]
plotQuote(thoughtR8.1.4, main="Dem 8 topics, Topic 4")
thoughtR8.1.5 <- findThoughts(GenDChoice, texts=outD$meta$GenD, n=10, topics=5)$docs[[1]]
plotQuote(thoughtR8.1.5, main="Dem 8 topics, Topic 5")
thoughtR8.1.6 <- findThoughts(GenDChoice, texts=outD$meta$GenD, n=10, topics=6)$docs[[1]]
plotQuote(thoughtR8.1.6, main="Dem 8 topics, Topic 6")
thoughtR8.1.7 <- findThoughts(GenDChoice, texts=outD$meta$GenD, n=10, topics=7)$docs[[1]]
plotQuote(thoughtR8.1.7, main="Dem 8 topics, Topic 7")
thoughtR8.1.8 <- findThoughts(GenDChoice, texts=outD$meta$GenD, n=10, topics=8)$docs[[1]]
plotQuote(thoughtR8.1.8, main="Dem 8 topics, Topic 8")

#Republican stereotype exemplars
thoughtR8.5.1 <- findThoughts(GenRChoice, texts=outR$meta$GenR, n=10, topics=1)$docs[[1]]
plotQuote(thoughtR8.5.1, main="Rep 8 topics, Topic 1")
thoughtR8.5.2 <- findThoughts(GenRChoice, texts=outR$meta$GenR, n=10, topics=2)$docs[[1]]
plotQuote(thoughtR8.5.2, main="Rep 8 topics, Topic 2")
thoughtR8.5.3 <- findThoughts(GenRChoice, texts=outR$meta$GenR, n=10, topics=3)$docs[[1]]
plotQuote(thoughtR8.5.3, main="Rep 8 topics, Topic 3")
thoughtR8.5.4 <- findThoughts(GenRChoice, texts=outR$meta$GenR, n=10, topics=4)$docs[[1]]
plotQuote(thoughtR8.5.4, main="Rep 8 topics, Topic 4")
thoughtR8.5.5 <- findThoughts(GenRChoice, texts=outR$meta$GenR, n=10, topics=5)$docs[[1]]
plotQuote(thoughtR8.5.5, main="Rep 8 topics, Topic 5")
thoughtR8.5.6 <- findThoughts(GenRChoice, texts=outR$meta$GenR, n=10, topics=6)$docs[[1]]
plotQuote(thoughtR8.5.6, main="Rep 8 topics, Topic 6")
thoughtR8.5.7 <- findThoughts(GenRChoice, texts=outR$meta$GenR, n=10, topics=7)$docs[[1]]
plotQuote(thoughtR8.5.7, main="Rep 8 topics, Topic 7")
thoughtR8.5.8 <- findThoughts(GenRChoice, texts=outR$meta$GenR, n=10, topics=8)$docs[[1]]
plotQuote(thoughtR8.5.8, main="Rep 8 topics, Topic 8")

####Appendix G:####
#Table #Proportion giving insincere responses (these are marked as 2 and 3):
#2016
#These are the same if you look at the mergedD or mergedR data objects
(table(mergedD3$PK_SoS_scored[mergedD3$year2021==0]))
prop.table(table(mergedD3$PK_SoS_scored[mergedD3$year2021==0]))
prop.table(table(mergedD3$PK_SoS_scored[mergedD3$year2021==0]))[3]+prop.table(table(mergedD3$PK_SoS_scored[mergedD3$year2021==0]))[4]

(table(mergedR3$PK_SoS_scored[mergedR3$year2021==0]))
prop.table(table(mergedR3$PK_SoS_scored[mergedD3$year2021==0]))
prop.table(table(mergedR3$PK_SoS_scored[mergedD3$year2021==0]))[3]+prop.table(table(mergedR3$PK_SoS_scored[mergedD3$year2021==0]))[4]

#2021
#These are the same if you look at the mergedD or mergedR data objects, with one additional
#non-sequitir response in the mergedR object.
table(mergedD3$PK_SoS_scored[mergedD3$year2021==1])
prop.table(table(mergedD3$PK_SoS_scored[mergedD3$year2021==1]))
prop.table(table(mergedD3$PK_SoS_scored[mergedD3$year2021==1]))[3]+prop.table(table(mergedD3$PK_SoS_scored[mergedD3$year2021==1]))[4]

table(mergedR3$PK_SoS_scored[mergedD3$year2021==1])
prop.table(table(mergedR3$PK_SoS_scored[mergedD3$year2021==1]))
prop.table(table(mergedR3$PK_SoS_scored[mergedD3$year2021==1]))[3]+prop.table(table(mergedR3$PK_SoS_scored[mergedD3$year2021==1]))[4]

#Topic use by insincere respondents:
temp=mergedD3[mergedD3$year2021==0,]
mean(temp$Topic7[temp$PK_SoS_scored==2|temp$PK_SoS_scored==3], na.rm=T)
temp=mergedD3[mergedD3$year2021==1,]
mean(temp$Topic7[temp$PK_SoS_scored==2|temp$PK_SoS_scored==3], na.rm=T)

temp=mergedR3[mergedR3$year2021==0,]
mean(temp$Topic8[temp$PK_SoS_scored==2|temp$PK_SoS_scored==3], na.rm=T)
temp=mergedR3[mergedR3$year2021==1,]
mean(temp$Topic8[temp$PK_SoS_scored==2|temp$PK_SoS_scored==3], na.rm=T)

#Figure G1
#Removing insincere responses
mergedD4=mergedD3[mergedD3$PK_SoS_scored!=2&mergedD3$PK_SoS_scored!=3,]
mergedR4=mergedR3[mergedR3$PK_SoS_scored!=2&mergedR3$PK_SoS_scored!=3,]

year=c(2016, 2016, 2016, 2016, 2021, 2021,2021, 2021)
party_temp=c("Republican", "Democrat", "Republican", "Democrat","Republican", "Democrat", "Republican", "Democrat")
weights=c("Weighted","Weighted", "Unweighted", "Unweighted","Weighted","Weighted", "Unweighted", "Unweighted")
means.graph=data.frame(cbind(year, party_temp, weights))

library(plotrix)
#D topic 1
means.graph$D1=NA
means.graph$D1.se=NA
means.graph$D1[1]=svyby(~Topic1, ~dem+rep, mergedD.weighted.2016, svymean)$Topic1[3]
means.graph$D1.se[1]=svyby(~Topic1, ~dem+rep, mergedD.weighted.2016, svymean)$se[3]
means.graph$D1[2]=svyby(~Topic1, ~dem+rep, mergedD.weighted.2016, svymean)$Topic1[2]
means.graph$D1.se[2]=svyby(~Topic1, ~dem+rep, mergedD.weighted.2016, svymean)$se[2]

means.graph$D1[3]=mean(mergedD4$Topic1[mergedD4$rep==1&mergedD4$year2021==0], na.rm=T)
means.graph$D1.se[3]=std.error(mergedD4$Topic1[mergedD4$rep==1&mergedD4$year2021==0])
means.graph$D1[4]=mean(mergedD4$Topic1[mergedD4$dem==1&mergedD4$year2021==0], na.rm=T)
means.graph$D1.se[4]=std.error(mergedD4$Topic1[mergedD4$dem==1&mergedD4$year2021==0])

means.graph$D1[5]=svyby(~Topic1, ~dem+rep, mergedD.weighted.2021, svymean)$Topic1[3]
means.graph$D1.se[5]=svyby(~Topic1, ~dem+rep, mergedD.weighted.2021, svymean)$se[3]
means.graph$D1[6]=svyby(~Topic1, ~dem+rep, mergedD.weighted.2021, svymean)$Topic1[2]
means.graph$D1.se[6]=svyby(~Topic1, ~dem+rep, mergedD.weighted.2021, svymean)$se[2]

means.graph$D1[7]=mean(mergedD4$Topic1[mergedD4$rep==1&mergedD4$year2021==1], na.rm=T)
means.graph$D1.se[7]=std.error(mergedD4$Topic1[mergedD4$rep==1&mergedD4$year2021==1])
means.graph$D1[8]=mean(mergedD4$Topic1[mergedD4$dem==1&mergedD4$year2021==1], na.rm=T)
means.graph$D1.se[8]=std.error(mergedD4$Topic1[mergedD4$dem==1&mergedD4$year2021==1])

#DTopic2
means.graph$D2=NA
means.graph$D2.se=NA
means.graph$D2[1]=svyby(~Topic2, ~dem+rep, mergedD.weighted.2016, svymean)$Topic2[3]
means.graph$D2.se[1]=svyby(~Topic2, ~dem+rep, mergedD.weighted.2016, svymean)$se[3]
means.graph$D2[2]=svyby(~Topic2, ~dem+rep, mergedD.weighted.2016, svymean)$Topic2[2]
means.graph$D2.se[2]=svyby(~Topic2, ~dem+rep, mergedD.weighted.2016, svymean)$se[2]

means.graph$D2[3]=mean(mergedD4$Topic2[mergedD4$rep==1&mergedD4$year2021==0], na.rm=T)
means.graph$D2.se[3]=std.error(mergedD4$Topic2[mergedD4$rep==1&mergedD4$year2021==0])
means.graph$D2[4]=mean(mergedD4$Topic2[mergedD4$dem==1&mergedD4$year2021==0], na.rm=T)
means.graph$D2.se[4]=std.error(mergedD4$Topic2[mergedD4$dem==1&mergedD4$year2021==0])

means.graph$D2[5]=svyby(~Topic2, ~dem+rep, mergedD.weighted.2021, svymean)$Topic2[3]
means.graph$D2.se[5]=svyby(~Topic2, ~dem+rep, mergedD.weighted.2021, svymean)$se[3]
means.graph$D2[6]=svyby(~Topic2, ~dem+rep, mergedD.weighted.2021, svymean)$Topic2[2]
means.graph$D2.se[6]=svyby(~Topic2, ~dem+rep, mergedD.weighted.2021, svymean)$se[2]

means.graph$D2[7]=mean(mergedD4$Topic2[mergedD4$rep==1&mergedD4$year2021==1], na.rm=T)
means.graph$D2.se[7]=std.error(mergedD4$Topic2[mergedD4$rep==1&mergedD4$year2021==1])
means.graph$D2[8]=mean(mergedD4$Topic2[mergedD4$dem==1&mergedD4$year2021==1], na.rm=T)
means.graph$D2.se[8]=std.error(mergedD4$Topic2[mergedD4$dem==1&mergedD4$year2021==1])

#DTopic3
means.graph$D3=NA
means.graph$D3.se=NA
means.graph$D3[1]=svyby(~Topic3, ~dem+rep, mergedD.weighted.2016, svymean)$Topic3[3]
means.graph$D3.se[1]=svyby(~Topic3, ~dem+rep, mergedD.weighted.2016, svymean)$se[3]
means.graph$D3[2]=svyby(~Topic3, ~dem+rep, mergedD.weighted.2016, svymean)$Topic3[2]
means.graph$D3.se[2]=svyby(~Topic3, ~dem+rep, mergedD.weighted.2016, svymean)$se[2]

means.graph$D3[3]=mean(mergedD4$Topic3[mergedD4$rep==1&mergedD4$year2021==0], na.rm=T)
means.graph$D3.se[3]=std.error(mergedD4$Topic3[mergedD4$rep==1&mergedD4$year2021==0])
means.graph$D3[4]=mean(mergedD4$Topic3[mergedD4$dem==1&mergedD4$year2021==0], na.rm=T)
means.graph$D3.se[4]=std.error(mergedD4$Topic3[mergedD4$dem==1&mergedD4$year2021==0])

means.graph$D3[5]=svyby(~Topic3, ~dem+rep, mergedD.weighted.2021, svymean)$Topic3[3]
means.graph$D3.se[5]=svyby(~Topic3, ~dem+rep, mergedD.weighted.2021, svymean)$se[3]
means.graph$D3[6]=svyby(~Topic3, ~dem+rep, mergedD.weighted.2021, svymean)$Topic3[2]
means.graph$D3.se[6]=svyby(~Topic3, ~dem+rep, mergedD.weighted.2021, svymean)$se[2]

means.graph$D3[7]=mean(mergedD4$Topic3[mergedD4$rep==1&mergedD4$year2021==1], na.rm=T)
means.graph$D3.se[7]=std.error(mergedD4$Topic3[mergedD4$rep==1&mergedD4$year2021==1])
means.graph$D3[8]=mean(mergedD4$Topic3[mergedD4$dem==1&mergedD4$year2021==1], na.rm=T)
means.graph$D3.se[8]=std.error(mergedD4$Topic3[mergedD4$dem==1&mergedD4$year2021==1])

#DTopic4
means.graph$D4=NA
means.graph$D4.se=NA
means.graph$D4[1]=svyby(~Topic4, ~dem+rep, mergedD.weighted.2016, svymean)$Topic4[3]
means.graph$D4.se[1]=svyby(~Topic4, ~dem+rep, mergedD.weighted.2016, svymean)$se[3]
means.graph$D4[2]=svyby(~Topic4, ~dem+rep, mergedD.weighted.2016, svymean)$Topic4[2]
means.graph$D4.se[2]=svyby(~Topic4, ~dem+rep, mergedD.weighted.2016, svymean)$se[2]

means.graph$D4[3]=mean(mergedD4$Topic4[mergedD4$rep==1&mergedD4$year2021==0], na.rm=T)
means.graph$D4.se[3]=std.error(mergedD4$Topic4[mergedD4$rep==1&mergedD4$year2021==0])
means.graph$D4[4]=mean(mergedD4$Topic4[mergedD4$dem==1&mergedD4$year2021==0], na.rm=T)
means.graph$D4.se[4]=std.error(mergedD4$Topic4[mergedD4$dem==1&mergedD4$year2021==0])

means.graph$D4[5]=svyby(~Topic4, ~dem+rep, mergedD.weighted.2021, svymean)$Topic4[3]
means.graph$D4.se[5]=svyby(~Topic4, ~dem+rep, mergedD.weighted.2021, svymean)$se[3]
means.graph$D4[6]=svyby(~Topic4, ~dem+rep, mergedD.weighted.2021, svymean)$Topic4[2]
means.graph$D4.se[6]=svyby(~Topic4, ~dem+rep, mergedD.weighted.2021, svymean)$se[2]

means.graph$D4[7]=mean(mergedD4$Topic4[mergedD4$rep==1&mergedD4$year2021==1], na.rm=T)
means.graph$D4.se[7]=std.error(mergedD4$Topic4[mergedD4$rep==1&mergedD4$year2021==1])
means.graph$D4[8]=mean(mergedD4$Topic4[mergedD4$dem==1&mergedD4$year2021==1], na.rm=T)
means.graph$D4.se[8]=std.error(mergedD4$Topic4[mergedD4$dem==1&mergedD4$year2021==1])

#DTopic5
means.graph$D5=NA
means.graph$D5.se=NA
means.graph$D5[1]=svyby(~Topic5, ~dem+rep, mergedD.weighted.2016, svymean)$Topic5[3]
means.graph$D5.se[1]=svyby(~Topic5, ~dem+rep, mergedD.weighted.2016, svymean)$se[3]
means.graph$D5[2]=svyby(~Topic5, ~dem+rep, mergedD.weighted.2016, svymean)$Topic5[2]
means.graph$D5.se[2]=svyby(~Topic5, ~dem+rep, mergedD.weighted.2016, svymean)$se[2]

means.graph$D5[3]=mean(mergedD4$Topic5[mergedD4$rep==1&mergedD4$year2021==0], na.rm=T)
means.graph$D5.se[3]=std.error(mergedD4$Topic5[mergedD4$rep==1&mergedD4$year2021==0])
means.graph$D5[4]=mean(mergedD4$Topic5[mergedD4$dem==1&mergedD4$year2021==0], na.rm=T)
means.graph$D5.se[4]=std.error(mergedD4$Topic5[mergedD4$dem==1&mergedD4$year2021==0])

means.graph$D5[5]=svyby(~Topic5, ~dem+rep, mergedD.weighted.2021, svymean)$Topic5[3]
means.graph$D5.se[5]=svyby(~Topic5, ~dem+rep, mergedD.weighted.2021, svymean)$se[3]
means.graph$D5[6]=svyby(~Topic5, ~dem+rep, mergedD.weighted.2021, svymean)$Topic5[2]
means.graph$D5.se[6]=svyby(~Topic5, ~dem+rep, mergedD.weighted.2021, svymean)$se[2]

means.graph$D5[7]=mean(mergedD4$Topic5[mergedD4$rep==1&mergedD4$year2021==1], na.rm=T)
means.graph$D5.se[7]=std.error(mergedD4$Topic5[mergedD4$rep==1&mergedD4$year2021==1])
means.graph$D5[8]=mean(mergedD4$Topic5[mergedD4$dem==1&mergedD4$year2021==1], na.rm=T)
means.graph$D5.se[8]=std.error(mergedD4$Topic5[mergedD4$dem==1&mergedD4$year2021==1])

#DTopic6
means.graph$D6=NA
means.graph$D6.se=NA
means.graph$D6[1]=svyby(~Topic6, ~dem+rep, mergedD.weighted.2016, svymean)$Topic6[3]
means.graph$D6.se[1]=svyby(~Topic6, ~dem+rep, mergedD.weighted.2016, svymean)$se[3]
means.graph$D6[2]=svyby(~Topic6, ~dem+rep, mergedD.weighted.2016, svymean)$Topic6[2]
means.graph$D6.se[2]=svyby(~Topic6, ~dem+rep, mergedD.weighted.2016, svymean)$se[2]

means.graph$D6[3]=mean(mergedD4$Topic6[mergedD4$rep==1&mergedD4$year2021==0], na.rm=T)
means.graph$D6.se[3]=std.error(mergedD4$Topic6[mergedD4$rep==1&mergedD4$year2021==0])
means.graph$D6[4]=mean(mergedD4$Topic6[mergedD4$dem==1&mergedD4$year2021==0], na.rm=T)
means.graph$D6.se[4]=std.error(mergedD4$Topic6[mergedD4$dem==1&mergedD4$year2021==0])

means.graph$D6[5]=svyby(~Topic6, ~dem+rep, mergedD.weighted.2021, svymean)$Topic6[3]
means.graph$D6.se[5]=svyby(~Topic6, ~dem+rep, mergedD.weighted.2021, svymean)$se[3]
means.graph$D6[6]=svyby(~Topic6, ~dem+rep, mergedD.weighted.2021, svymean)$Topic6[2]
means.graph$D6.se[6]=svyby(~Topic6, ~dem+rep, mergedD.weighted.2021, svymean)$se[2]

means.graph$D6[7]=mean(mergedD4$Topic6[mergedD4$rep==1&mergedD4$year2021==1], na.rm=T)
means.graph$D6.se[7]=std.error(mergedD4$Topic6[mergedD4$rep==1&mergedD4$year2021==1])
means.graph$D6[8]=mean(mergedD4$Topic6[mergedD4$dem==1&mergedD4$year2021==1], na.rm=T)
means.graph$D6.se[8]=std.error(mergedD4$Topic6[mergedD4$dem==1&mergedD4$year2021==1])

#DTopic7
means.graph$D7=NA
means.graph$D7.se=NA
means.graph$D7[1]=svyby(~Topic7, ~dem+rep, mergedD.weighted.2016, svymean)$Topic7[3]
means.graph$D7.se[1]=svyby(~Topic7, ~dem+rep, mergedD.weighted.2016, svymean)$se[3]
means.graph$D7[2]=svyby(~Topic7, ~dem+rep, mergedD.weighted.2016, svymean)$Topic7[2]
means.graph$D7.se[2]=svyby(~Topic7, ~dem+rep, mergedD.weighted.2016, svymean)$se[2]

means.graph$D7[3]=mean(mergedD4$Topic7[mergedD4$rep==1&mergedD4$year2021==0], na.rm=T)
means.graph$D7.se[3]=std.error(mergedD4$Topic7[mergedD4$rep==1&mergedD4$year2021==0])
means.graph$D7[4]=mean(mergedD4$Topic7[mergedD4$dem==1&mergedD4$year2021==0], na.rm=T)
means.graph$D7.se[4]=std.error(mergedD4$Topic7[mergedD4$dem==1&mergedD4$year2021==0])

means.graph$D7[5]=svyby(~Topic7, ~dem+rep, mergedD.weighted.2021, svymean)$Topic7[3]
means.graph$D7.se[5]=svyby(~Topic7, ~dem+rep, mergedD.weighted.2021, svymean)$se[3]
means.graph$D7[6]=svyby(~Topic7, ~dem+rep, mergedD.weighted.2021, svymean)$Topic7[2]
means.graph$D7.se[6]=svyby(~Topic7, ~dem+rep, mergedD.weighted.2021, svymean)$se[2]

means.graph$D7[7]=mean(mergedD4$Topic7[mergedD4$rep==1&mergedD4$year2021==1], na.rm=T)
means.graph$D7.se[7]=std.error(mergedD4$Topic7[mergedD4$rep==1&mergedD4$year2021==1])
means.graph$D7[8]=mean(mergedD4$Topic7[mergedD4$dem==1&mergedD4$year2021==1], na.rm=T)
means.graph$D7.se[8]=std.error(mergedD4$Topic7[mergedD4$dem==1&mergedD4$year2021==1])

#DTopic8
means.graph$D8=NA
means.graph$D8.se=NA
means.graph$D8[1]=svyby(~Topic8, ~dem+rep, mergedD.weighted.2016, svymean)$Topic8[3]
means.graph$D8.se[1]=svyby(~Topic8, ~dem+rep, mergedD.weighted.2016, svymean)$se[3]
means.graph$D8[2]=svyby(~Topic8, ~dem+rep, mergedD.weighted.2016, svymean)$Topic8[2]
means.graph$D8.se[2]=svyby(~Topic8, ~dem+rep, mergedD.weighted.2016, svymean)$se[2]

means.graph$D8[3]=mean(mergedD4$Topic8[mergedD4$rep==1&mergedD4$year2021==0], na.rm=T)
means.graph$D8.se[3]=std.error(mergedD4$Topic8[mergedD4$rep==1&mergedD4$year2021==0])
means.graph$D8[4]=mean(mergedD4$Topic8[mergedD4$dem==1&mergedD4$year2021==0], na.rm=T)
means.graph$D8.se[4]=std.error(mergedD4$Topic8[mergedD4$dem==1&mergedD4$year2021==0])

means.graph$D8[5]=svyby(~Topic8, ~dem+rep, mergedD.weighted.2021, svymean)$Topic8[3]
means.graph$D8.se[5]=svyby(~Topic8, ~dem+rep, mergedD.weighted.2021, svymean)$se[3]
means.graph$D8[6]=svyby(~Topic8, ~dem+rep, mergedD.weighted.2021, svymean)$Topic8[2]
means.graph$D8.se[6]=svyby(~Topic8, ~dem+rep, mergedD.weighted.2021, svymean)$se[2]

means.graph$D8[7]=mean(mergedD4$Topic8[mergedD4$rep==1&mergedD4$year2021==1], na.rm=T)
means.graph$D8.se[7]=std.error(mergedD4$Topic8[mergedD4$rep==1&mergedD4$year2021==1])
means.graph$D8[8]=mean(mergedD4$Topic8[mergedD4$dem==1&mergedD4$year2021==1], na.rm=T)
means.graph$D8.se[8]=std.error(mergedD4$Topic8[mergedD4$dem==1&mergedD4$year2021==1])

#Republican topics
#R Topic 1
means.graph$R1=NA
means.graph$R1.se=NA
means.graph$R1[1]=svyby(~Topic1, ~dem+rep, mergedR.weighted.2016, svymean)$Topic1[3]
means.graph$R1.se[1]=svyby(~Topic1, ~dem+rep, mergedR.weighted.2016, svymean)$se[3]
means.graph$R1[2]=svyby(~Topic1, ~dem+rep, mergedR.weighted.2016, svymean)$Topic1[2]
means.graph$R1.se[2]=svyby(~Topic1, ~dem+rep, mergedR.weighted.2016, svymean)$se[2]

means.graph$R1[3]=mean(mergedR4$Topic1[mergedR4$rep==1&mergedR4$year2021==0], na.rm=T)
means.graph$R1.se[3]=std.error(mergedR4$Topic1[mergedR4$rep==1&mergedR4$year2021==0])
means.graph$R1[4]=mean(mergedR4$Topic1[mergedR4$dem==1&mergedR4$year2021==0], na.rm=T)
means.graph$R1.se[4]=std.error(mergedR4$Topic1[mergedR4$dem==1&mergedR4$year2021==0])

means.graph$R1[5]=svyby(~Topic1, ~dem+rep, mergedR.weighted.2021, svymean)$Topic1[3]
means.graph$R1.se[5]=svyby(~Topic1, ~dem+rep, mergedR.weighted.2021, svymean)$se[3]
means.graph$R1[6]=svyby(~Topic1, ~dem+rep, mergedR.weighted.2021, svymean)$Topic1[2]
means.graph$R1.se[6]=svyby(~Topic1, ~dem+rep, mergedR.weighted.2021, svymean)$se[2]

means.graph$R1[7]=mean(mergedR4$Topic1[mergedR4$rep==1&mergedR4$year2021==1], na.rm=T)
means.graph$R1.se[7]=std.error(mergedR4$Topic1[mergedR4$rep==1&mergedR4$year2021==1])
means.graph$R1[8]=mean(mergedR4$Topic1[mergedR4$dem==1&mergedR4$year2021==1], na.rm=T)
means.graph$R1.se[8]=std.error(mergedR4$Topic1[mergedR4$dem==1&mergedR4$year2021==1])

#RTopic2
means.graph$R2=NA
means.graph$R2.se=NA
means.graph$R2[1]=svyby(~Topic2, ~dem+rep, mergedR.weighted.2016, svymean)$Topic2[3]
means.graph$R2.se[1]=svyby(~Topic2, ~dem+rep, mergedR.weighted.2016, svymean)$se[3]
means.graph$R2[2]=svyby(~Topic2, ~dem+rep, mergedR.weighted.2016, svymean)$Topic2[2]
means.graph$R2.se[2]=svyby(~Topic2, ~dem+rep, mergedR.weighted.2016, svymean)$se[2]

means.graph$R2[3]=mean(mergedR4$Topic2[mergedR4$rep==1&mergedR4$year2021==0], na.rm=T)
means.graph$R2.se[3]=std.error(mergedR4$Topic2[mergedR4$rep==1&mergedR4$year2021==0])
means.graph$R2[4]=mean(mergedR4$Topic2[mergedR4$dem==1&mergedR4$year2021==0], na.rm=T)
means.graph$R2.se[4]=std.error(mergedR4$Topic2[mergedR4$dem==1&mergedR4$year2021==0])

means.graph$R2[5]=svyby(~Topic2, ~dem+rep, mergedR.weighted.2021, svymean)$Topic2[3]
means.graph$R2.se[5]=svyby(~Topic2, ~dem+rep, mergedR.weighted.2021, svymean)$se[3]
means.graph$R2[6]=svyby(~Topic2, ~dem+rep, mergedR.weighted.2021, svymean)$Topic2[2]
means.graph$R2.se[6]=svyby(~Topic2, ~dem+rep, mergedR.weighted.2021, svymean)$se[2]

means.graph$R2[7]=mean(mergedR4$Topic2[mergedR4$rep==1&mergedR4$year2021==1], na.rm=T)
means.graph$R2.se[7]=std.error(mergedR4$Topic2[mergedR4$rep==1&mergedR4$year2021==1])
means.graph$R2[8]=mean(mergedR4$Topic2[mergedR4$dem==1&mergedR4$year2021==1], na.rm=T)
means.graph$R2.se[8]=std.error(mergedR4$Topic2[mergedR4$dem==1&mergedR4$year2021==1])

#RTopic3
means.graph$R3=NA
means.graph$R3.se=NA
means.graph$R3[1]=svyby(~Topic3, ~dem+rep, mergedR.weighted.2016, svymean)$Topic3[3]
means.graph$R3.se[1]=svyby(~Topic3, ~dem+rep, mergedR.weighted.2016, svymean)$se[3]
means.graph$R3[2]=svyby(~Topic3, ~dem+rep, mergedR.weighted.2016, svymean)$Topic3[2]
means.graph$R3.se[2]=svyby(~Topic3, ~dem+rep, mergedR.weighted.2016, svymean)$se[2]

means.graph$R3[3]=mean(mergedR4$Topic3[mergedR4$rep==1&mergedR4$year2021==0], na.rm=T)
means.graph$R3.se[3]=std.error(mergedR4$Topic3[mergedR4$rep==1&mergedR4$year2021==0])
means.graph$R3[4]=mean(mergedR4$Topic3[mergedR4$dem==1&mergedR4$year2021==0], na.rm=T)
means.graph$R3.se[4]=std.error(mergedR4$Topic3[mergedR4$dem==1&mergedR4$year2021==0])

means.graph$R3[5]=svyby(~Topic3, ~dem+rep, mergedR.weighted.2021, svymean)$Topic3[3]
means.graph$R3.se[5]=svyby(~Topic3, ~dem+rep, mergedR.weighted.2021, svymean)$se[3]
means.graph$R3[6]=svyby(~Topic3, ~dem+rep, mergedR.weighted.2021, svymean)$Topic3[2]
means.graph$R3.se[6]=svyby(~Topic3, ~dem+rep, mergedR.weighted.2021, svymean)$se[2]

means.graph$R3[7]=mean(mergedR4$Topic3[mergedR4$rep==1&mergedR4$year2021==1], na.rm=T)
means.graph$R3.se[7]=std.error(mergedR4$Topic3[mergedR4$rep==1&mergedR4$year2021==1])
means.graph$R3[8]=mean(mergedR4$Topic3[mergedR4$dem==1&mergedR4$year2021==1], na.rm=T)
means.graph$R3.se[8]=std.error(mergedR4$Topic3[mergedR4$dem==1&mergedR4$year2021==1])

#RTopic4
means.graph$R4=NA
means.graph$R4.se=NA
means.graph$R4[1]=svyby(~Topic4, ~dem+rep, mergedR.weighted.2016, svymean)$Topic4[3]
means.graph$R4.se[1]=svyby(~Topic4, ~dem+rep, mergedR.weighted.2016, svymean)$se[3]
means.graph$R4[2]=svyby(~Topic4, ~dem+rep, mergedR.weighted.2016, svymean)$Topic4[2]
means.graph$R4.se[2]=svyby(~Topic4, ~dem+rep, mergedR.weighted.2016, svymean)$se[2]

means.graph$R4[3]=mean(mergedR4$Topic4[mergedR4$rep==1&mergedR4$year2021==0], na.rm=T)
means.graph$R4.se[3]=std.error(mergedR4$Topic4[mergedR4$rep==1&mergedR4$year2021==0])
means.graph$R4[4]=mean(mergedR4$Topic4[mergedR4$dem==1&mergedR4$year2021==0], na.rm=T)
means.graph$R4.se[4]=std.error(mergedR4$Topic4[mergedR4$dem==1&mergedR4$year2021==0])

means.graph$R4[5]=svyby(~Topic4, ~dem+rep, mergedR.weighted.2021, svymean)$Topic4[3]
means.graph$R4.se[5]=svyby(~Topic4, ~dem+rep, mergedR.weighted.2021, svymean)$se[3]
means.graph$R4[6]=svyby(~Topic4, ~dem+rep, mergedR.weighted.2021, svymean)$Topic4[2]
means.graph$R4.se[6]=svyby(~Topic4, ~dem+rep, mergedR.weighted.2021, svymean)$se[2]

means.graph$R4[7]=mean(mergedR4$Topic4[mergedR4$rep==1&mergedR4$year2021==1], na.rm=T)
means.graph$R4.se[7]=std.error(mergedR4$Topic4[mergedR4$rep==1&mergedR4$year2021==1])
means.graph$R4[8]=mean(mergedR4$Topic4[mergedR4$dem==1&mergedR4$year2021==1], na.rm=T)
means.graph$R4.se[8]=std.error(mergedR4$Topic4[mergedR4$dem==1&mergedR4$year2021==1])

#RTopic5
means.graph$R5=NA
means.graph$R5.se=NA
means.graph$R5[1]=svyby(~Topic5, ~dem+rep, mergedR.weighted.2016, svymean)$Topic5[3]
means.graph$R5.se[1]=svyby(~Topic5, ~dem+rep, mergedR.weighted.2016, svymean)$se[3]
means.graph$R5[2]=svyby(~Topic5, ~dem+rep, mergedR.weighted.2016, svymean)$Topic5[2]
means.graph$R5.se[2]=svyby(~Topic5, ~dem+rep, mergedR.weighted.2016, svymean)$se[2]

means.graph$R5[3]=mean(mergedR4$Topic5[mergedR4$rep==1&mergedR4$year2021==0], na.rm=T)
means.graph$R5.se[3]=std.error(mergedR4$Topic5[mergedR4$rep==1&mergedR4$year2021==0])
means.graph$R5[4]=mean(mergedR4$Topic5[mergedR4$dem==1&mergedR4$year2021==0], na.rm=T)
means.graph$R5.se[4]=std.error(mergedR4$Topic5[mergedR4$dem==1&mergedR4$year2021==0])

means.graph$R5[5]=svyby(~Topic5, ~dem+rep, mergedR.weighted.2021, svymean)$Topic5[3]
means.graph$R5.se[5]=svyby(~Topic5, ~dem+rep, mergedR.weighted.2021, svymean)$se[3]
means.graph$R5[6]=svyby(~Topic5, ~dem+rep, mergedR.weighted.2021, svymean)$Topic5[2]
means.graph$R5.se[6]=svyby(~Topic5, ~dem+rep, mergedR.weighted.2021, svymean)$se[2]

means.graph$R5[7]=mean(mergedR4$Topic5[mergedR4$rep==1&mergedR4$year2021==1], na.rm=T)
means.graph$R5.se[7]=std.error(mergedR4$Topic5[mergedR4$rep==1&mergedR4$year2021==1])
means.graph$R5[8]=mean(mergedR4$Topic5[mergedR4$dem==1&mergedR4$year2021==1], na.rm=T)
means.graph$R5.se[8]=std.error(mergedR4$Topic5[mergedR4$dem==1&mergedR4$year2021==1])

#RTopic6
means.graph$R6=NA
means.graph$R6.se=NA
means.graph$R6[1]=svyby(~Topic6, ~dem+rep, mergedR.weighted.2016, svymean)$Topic6[3]
means.graph$R6.se[1]=svyby(~Topic6, ~dem+rep, mergedR.weighted.2016, svymean)$se[3]
means.graph$R6[2]=svyby(~Topic6, ~dem+rep, mergedR.weighted.2016, svymean)$Topic6[2]
means.graph$R6.se[2]=svyby(~Topic6, ~dem+rep, mergedR.weighted.2016, svymean)$se[2]

means.graph$R6[3]=mean(mergedR4$Topic6[mergedR4$rep==1&mergedR4$year2021==0], na.rm=T)
means.graph$R6.se[3]=std.error(mergedR4$Topic6[mergedR4$rep==1&mergedR4$year2021==0])
means.graph$R6[4]=mean(mergedR4$Topic6[mergedR4$dem==1&mergedR4$year2021==0], na.rm=T)
means.graph$R6.se[4]=std.error(mergedR4$Topic6[mergedR4$dem==1&mergedR4$year2021==0])

means.graph$R6[5]=svyby(~Topic6, ~dem+rep, mergedR.weighted.2021, svymean)$Topic6[3]
means.graph$R6.se[5]=svyby(~Topic6, ~dem+rep, mergedR.weighted.2021, svymean)$se[3]
means.graph$R6[6]=svyby(~Topic6, ~dem+rep, mergedR.weighted.2021, svymean)$Topic6[2]
means.graph$R6.se[6]=svyby(~Topic6, ~dem+rep, mergedR.weighted.2021, svymean)$se[2]

means.graph$R6[7]=mean(mergedR4$Topic6[mergedR4$rep==1&mergedR4$year2021==1], na.rm=T)
means.graph$R6.se[7]=std.error(mergedR4$Topic6[mergedR4$rep==1&mergedR4$year2021==1])
means.graph$R6[8]=mean(mergedR4$Topic6[mergedR4$dem==1&mergedR4$year2021==1], na.rm=T)
means.graph$R6.se[8]=std.error(mergedR4$Topic6[mergedR4$dem==1&mergedR4$year2021==1])

#RTopic7
means.graph$R7=NA
means.graph$R7.se=NA
means.graph$R7[1]=svyby(~Topic7, ~dem+rep, mergedR.weighted.2016, svymean)$Topic7[3]
means.graph$R7.se[1]=svyby(~Topic7, ~dem+rep, mergedR.weighted.2016, svymean)$se[3]
means.graph$R7[2]=svyby(~Topic7, ~dem+rep, mergedR.weighted.2016, svymean)$Topic7[2]
means.graph$R7.se[2]=svyby(~Topic7, ~dem+rep, mergedR.weighted.2016, svymean)$se[2]

means.graph$R7[3]=mean(mergedR4$Topic7[mergedR4$rep==1&mergedR4$year2021==0], na.rm=T)
means.graph$R7.se[3]=std.error(mergedR4$Topic7[mergedR4$rep==1&mergedR4$year2021==0])
means.graph$R7[4]=mean(mergedR4$Topic7[mergedR4$dem==1&mergedR4$year2021==0], na.rm=T)
means.graph$R7.se[4]=std.error(mergedR4$Topic7[mergedR4$dem==1&mergedR4$year2021==0])

means.graph$R7[5]=svyby(~Topic7, ~dem+rep, mergedR.weighted.2021, svymean)$Topic7[3]
means.graph$R7.se[5]=svyby(~Topic7, ~dem+rep, mergedR.weighted.2021, svymean)$se[3]
means.graph$R7[6]=svyby(~Topic7, ~dem+rep, mergedR.weighted.2021, svymean)$Topic7[2]
means.graph$R7.se[6]=svyby(~Topic7, ~dem+rep, mergedR.weighted.2021, svymean)$se[2]

means.graph$R7[7]=mean(mergedR4$Topic7[mergedR4$rep==1&mergedR4$year2021==1], na.rm=T)
means.graph$R7.se[7]=std.error(mergedR4$Topic7[mergedR4$rep==1&mergedR4$year2021==1])
means.graph$R7[8]=mean(mergedR4$Topic7[mergedR4$dem==1&mergedR4$year2021==1], na.rm=T)
means.graph$R7.se[8]=std.error(mergedR4$Topic7[mergedR4$dem==1&mergedR4$year2021==1])

#RTopic8
means.graph$R8=NA
means.graph$R8.se=NA
means.graph$R8[1]=svyby(~Topic8, ~dem+rep, mergedR.weighted.2016, svymean)$Topic8[3]
means.graph$R8.se[1]=svyby(~Topic8, ~dem+rep, mergedR.weighted.2016, svymean)$se[3]
means.graph$R8[2]=svyby(~Topic8, ~dem+rep, mergedR.weighted.2016, svymean)$Topic8[2]
means.graph$R8.se[2]=svyby(~Topic8, ~dem+rep, mergedR.weighted.2016, svymean)$se[2]

means.graph$R8[3]=mean(mergedR4$Topic8[mergedR4$rep==1&mergedR4$year2021==0], na.rm=T)
means.graph$R8.se[3]=std.error(mergedR4$Topic8[mergedR4$rep==1&mergedR4$year2021==0])
means.graph$R8[4]=mean(mergedR4$Topic8[mergedR4$dem==1&mergedR4$year2021==0], na.rm=T)
means.graph$R8.se[4]=std.error(mergedR4$Topic8[mergedR4$dem==1&mergedR4$year2021==0])

means.graph$R8[5]=svyby(~Topic8, ~dem+rep, mergedR.weighted.2021, svymean)$Topic8[3]
means.graph$R8.se[5]=svyby(~Topic8, ~dem+rep, mergedR.weighted.2021, svymean)$se[3]
means.graph$R8[6]=svyby(~Topic8, ~dem+rep, mergedR.weighted.2021, svymean)$Topic8[2]
means.graph$R8.se[6]=svyby(~Topic8, ~dem+rep, mergedR.weighted.2021, svymean)$se[2]

means.graph$R8[7]=mean(mergedR4$Topic8[mergedR4$rep==1&mergedR4$year2021==1], na.rm=T)
means.graph$R8.se[7]=std.error(mergedR4$Topic8[mergedR4$rep==1&mergedR4$year2021==1])
means.graph$R8[8]=mean(mergedR4$Topic8[mergedR4$dem==1&mergedR4$year2021==1], na.rm=T)
means.graph$R8.se[8]=std.error(mergedR4$Topic8[mergedR4$dem==1&mergedR4$year2021==1])

##Unweighted results##
#Make this into a similar structure as Figure 1
party=c(rep(c("Stereotypes of Republicans", "Stereotypes of Republicans"),16),
        rep(c("Stereotypes of Democrats", "Stereotypes of Democrats"),16)
)
respondent=c(rep("Republican respondents",16), 
             rep("Democratic respondents",16),
             rep("Republican respondents",16), 
             rep("Democratic respondents",16))

topic=c(1,1,
        2,2,
        3,3,
        4,4,
        5,5,
        6,6,
        7,7,
        8,8,
        1,1,
        2,2,
        3,3,
        4,4,
        5,5,
        6,6,
        7,7,
        8,8,
        1,1,
        2,2,
        3,3,
        4,4,
        5,5,
        6,6,
        7,7,
        8,8,
        1,1,
        2,2,
        3,3,
        4,4,
        5,5,
        6,6,
        7,7,
        8,8
)

bar=c(means.graph$R1[means.graph$year==2016&means.graph$party_temp=="Republican"&means.graph$weights=="Unweighted"],
      means.graph$R1[means.graph$year==2021&means.graph$party_temp=="Republican"&means.graph$weights=="Unweighted"],
      means.graph$R2[means.graph$year==2016&means.graph$party_temp=="Republican"&means.graph$weights=="Unweighted"],
      means.graph$R2[means.graph$year==2021&means.graph$party_temp=="Republican"&means.graph$weights=="Unweighted"],
      means.graph$R3[means.graph$year==2016&means.graph$party_temp=="Republican"&means.graph$weights=="Unweighted"],
      means.graph$R3[means.graph$year==2021&means.graph$party_temp=="Republican"&means.graph$weights=="Unweighted"],
      means.graph$R4[means.graph$year==2016&means.graph$party_temp=="Republican"&means.graph$weights=="Unweighted"],
      means.graph$R4[means.graph$year==2021&means.graph$party_temp=="Republican"&means.graph$weights=="Unweighted"],
      means.graph$R5[means.graph$year==2016&means.graph$party_temp=="Republican"&means.graph$weights=="Unweighted"],
      means.graph$R5[means.graph$year==2021&means.graph$party_temp=="Republican"&means.graph$weights=="Unweighted"],
      means.graph$R6[means.graph$year==2016&means.graph$party_temp=="Republican"&means.graph$weights=="Unweighted"],
      means.graph$R6[means.graph$year==2021&means.graph$party_temp=="Republican"&means.graph$weights=="Unweighted"],
      means.graph$R7[means.graph$year==2016&means.graph$party_temp=="Republican"&means.graph$weights=="Unweighted"],
      means.graph$R7[means.graph$year==2021&means.graph$party_temp=="Republican"&means.graph$weights=="Unweighted"],
      means.graph$R8[means.graph$year==2016&means.graph$party_temp=="Republican"&means.graph$weights=="Unweighted"],
      means.graph$R8[means.graph$year==2021&means.graph$party_temp=="Republican"&means.graph$weights=="Unweighted"],
      means.graph$R1[means.graph$year==2016&means.graph$party_temp=="Democrat"&means.graph$weights=="Unweighted"],
      means.graph$R1[means.graph$year==2021&means.graph$party_temp=="Democrat"&means.graph$weights=="Unweighted"],
      means.graph$R2[means.graph$year==2016&means.graph$party_temp=="Democrat"&means.graph$weights=="Unweighted"],
      means.graph$R2[means.graph$year==2021&means.graph$party_temp=="Democrat"&means.graph$weights=="Unweighted"],
      means.graph$R3[means.graph$year==2016&means.graph$party_temp=="Democrat"&means.graph$weights=="Unweighted"],
      means.graph$R3[means.graph$year==2021&means.graph$party_temp=="Democrat"&means.graph$weights=="Unweighted"],
      means.graph$R4[means.graph$year==2016&means.graph$party_temp=="Democrat"&means.graph$weights=="Unweighted"],
      means.graph$R4[means.graph$year==2021&means.graph$party_temp=="Democrat"&means.graph$weights=="Unweighted"],
      means.graph$R5[means.graph$year==2016&means.graph$party_temp=="Democrat"&means.graph$weights=="Unweighted"],
      means.graph$R5[means.graph$year==2021&means.graph$party_temp=="Democrat"&means.graph$weights=="Unweighted"],
      means.graph$R6[means.graph$year==2016&means.graph$party_temp=="Democrat"&means.graph$weights=="Unweighted"],
      means.graph$R6[means.graph$year==2021&means.graph$party_temp=="Democrat"&means.graph$weights=="Unweighted"],
      means.graph$R7[means.graph$year==2016&means.graph$party_temp=="Democrat"&means.graph$weights=="Unweighted"],
      means.graph$R7[means.graph$year==2021&means.graph$party_temp=="Democrat"&means.graph$weights=="Unweighted"],
      means.graph$R8[means.graph$year==2016&means.graph$party_temp=="Democrat"&means.graph$weights=="Unweighted"],
      means.graph$R8[means.graph$year==2021&means.graph$party_temp=="Democrat"&means.graph$weights=="Unweighted"],
      means.graph$D1[means.graph$year==2016&means.graph$party_temp=="Republican"&means.graph$weights=="Unweighted"],
      means.graph$D1[means.graph$year==2021&means.graph$party_temp=="Republican"&means.graph$weights=="Unweighted"],
      means.graph$D2[means.graph$year==2016&means.graph$party_temp=="Republican"&means.graph$weights=="Unweighted"],
      means.graph$D2[means.graph$year==2021&means.graph$party_temp=="Republican"&means.graph$weights=="Unweighted"],
      means.graph$D3[means.graph$year==2016&means.graph$party_temp=="Republican"&means.graph$weights=="Unweighted"],
      means.graph$D3[means.graph$year==2021&means.graph$party_temp=="Republican"&means.graph$weights=="Unweighted"],
      means.graph$D4[means.graph$year==2016&means.graph$party_temp=="Republican"&means.graph$weights=="Unweighted"],
      means.graph$D4[means.graph$year==2021&means.graph$party_temp=="Republican"&means.graph$weights=="Unweighted"],
      means.graph$D5[means.graph$year==2016&means.graph$party_temp=="Republican"&means.graph$weights=="Unweighted"],
      means.graph$D5[means.graph$year==2021&means.graph$party_temp=="Republican"&means.graph$weights=="Unweighted"],
      means.graph$D6[means.graph$year==2016&means.graph$party_temp=="Republican"&means.graph$weights=="Unweighted"],
      means.graph$D6[means.graph$year==2021&means.graph$party_temp=="Republican"&means.graph$weights=="Unweighted"],
      means.graph$D7[means.graph$year==2016&means.graph$party_temp=="Republican"&means.graph$weights=="Unweighted"],
      means.graph$D7[means.graph$year==2021&means.graph$party_temp=="Republican"&means.graph$weights=="Unweighted"],
      means.graph$D8[means.graph$year==2016&means.graph$party_temp=="Republican"&means.graph$weights=="Unweighted"],
      means.graph$D8[means.graph$year==2021&means.graph$party_temp=="Republican"&means.graph$weights=="Unweighted"],
      means.graph$D1[means.graph$year==2016&means.graph$party_temp=="Democrat"&means.graph$weights=="Unweighted"],
      means.graph$D1[means.graph$year==2021&means.graph$party_temp=="Democrat"&means.graph$weights=="Unweighted"],
      means.graph$D2[means.graph$year==2016&means.graph$party_temp=="Democrat"&means.graph$weights=="Unweighted"],
      means.graph$D2[means.graph$year==2021&means.graph$party_temp=="Democrat"&means.graph$weights=="Unweighted"],
      means.graph$D3[means.graph$year==2016&means.graph$party_temp=="Democrat"&means.graph$weights=="Unweighted"],
      means.graph$D3[means.graph$year==2021&means.graph$party_temp=="Democrat"&means.graph$weights=="Unweighted"],
      means.graph$D4[means.graph$year==2016&means.graph$party_temp=="Democrat"&means.graph$weights=="Unweighted"],
      means.graph$D4[means.graph$year==2021&means.graph$party_temp=="Democrat"&means.graph$weights=="Unweighted"],
      means.graph$D5[means.graph$year==2016&means.graph$party_temp=="Democrat"&means.graph$weights=="Unweighted"],
      means.graph$D5[means.graph$year==2021&means.graph$party_temp=="Democrat"&means.graph$weights=="Unweighted"],
      means.graph$D6[means.graph$year==2016&means.graph$party_temp=="Democrat"&means.graph$weights=="Unweighted"],
      means.graph$D6[means.graph$year==2021&means.graph$party_temp=="Democrat"&means.graph$weights=="Unweighted"],
      means.graph$D7[means.graph$year==2016&means.graph$party_temp=="Democrat"&means.graph$weights=="Unweighted"],
      means.graph$D7[means.graph$year==2021&means.graph$party_temp=="Democrat"&means.graph$weights=="Unweighted"],
      means.graph$D8[means.graph$year==2016&means.graph$party_temp=="Democrat"&means.graph$weights=="Unweighted"],
      means.graph$D8[means.graph$year==2021&means.graph$party_temp=="Democrat"&means.graph$weights=="Unweighted"]
      
)

se=c(
  means.graph$R1.se[means.graph$year==2016&means.graph$party_temp=="Republican"&means.graph$weights=="Unweighted"],
  means.graph$R1.se[means.graph$year==2021&means.graph$party_temp=="Republican"&means.graph$weights=="Unweighted"],
  means.graph$R2.se[means.graph$year==2016&means.graph$party_temp=="Republican"&means.graph$weights=="Unweighted"],
  means.graph$R2.se[means.graph$year==2021&means.graph$party_temp=="Republican"&means.graph$weights=="Unweighted"],
  means.graph$R3.se[means.graph$year==2016&means.graph$party_temp=="Republican"&means.graph$weights=="Unweighted"],
  means.graph$R3.se[means.graph$year==2021&means.graph$party_temp=="Republican"&means.graph$weights=="Unweighted"],
  means.graph$R4.se[means.graph$year==2016&means.graph$party_temp=="Republican"&means.graph$weights=="Unweighted"],
  means.graph$R4.se[means.graph$year==2021&means.graph$party_temp=="Republican"&means.graph$weights=="Unweighted"],
  means.graph$R5.se[means.graph$year==2016&means.graph$party_temp=="Republican"&means.graph$weights=="Unweighted"],
  means.graph$R5.se[means.graph$year==2021&means.graph$party_temp=="Republican"&means.graph$weights=="Unweighted"],
  means.graph$R6.se[means.graph$year==2016&means.graph$party_temp=="Republican"&means.graph$weights=="Unweighted"],
  means.graph$R6.se[means.graph$year==2021&means.graph$party_temp=="Republican"&means.graph$weights=="Unweighted"],
  means.graph$R7.se[means.graph$year==2016&means.graph$party_temp=="Republican"&means.graph$weights=="Unweighted"],
  means.graph$R7.se[means.graph$year==2021&means.graph$party_temp=="Republican"&means.graph$weights=="Unweighted"],
  means.graph$R8.se[means.graph$year==2016&means.graph$party_temp=="Republican"&means.graph$weights=="Unweighted"],
  means.graph$R8.se[means.graph$year==2021&means.graph$party_temp=="Republican"&means.graph$weights=="Unweighted"],
  means.graph$R1.se[means.graph$year==2016&means.graph$party_temp=="Democrat"&means.graph$weights=="Unweighted"],
  means.graph$R1.se[means.graph$year==2021&means.graph$party_temp=="Democrat"&means.graph$weights=="Unweighted"],
  means.graph$R2.se[means.graph$year==2016&means.graph$party_temp=="Democrat"&means.graph$weights=="Unweighted"],
  means.graph$R2.se[means.graph$year==2021&means.graph$party_temp=="Democrat"&means.graph$weights=="Unweighted"],
  means.graph$R3.se[means.graph$year==2016&means.graph$party_temp=="Democrat"&means.graph$weights=="Unweighted"],
  means.graph$R3.se[means.graph$year==2021&means.graph$party_temp=="Democrat"&means.graph$weights=="Unweighted"],
  means.graph$R4.se[means.graph$year==2016&means.graph$party_temp=="Democrat"&means.graph$weights=="Unweighted"],
  means.graph$R4.se[means.graph$year==2021&means.graph$party_temp=="Democrat"&means.graph$weights=="Unweighted"],
  means.graph$R5.se[means.graph$year==2016&means.graph$party_temp=="Democrat"&means.graph$weights=="Unweighted"],
  means.graph$R5.se[means.graph$year==2021&means.graph$party_temp=="Democrat"&means.graph$weights=="Unweighted"],
  means.graph$R6.se[means.graph$year==2016&means.graph$party_temp=="Democrat"&means.graph$weights=="Unweighted"],
  means.graph$R6.se[means.graph$year==2021&means.graph$party_temp=="Democrat"&means.graph$weights=="Unweighted"],
  means.graph$R7.se[means.graph$year==2016&means.graph$party_temp=="Democrat"&means.graph$weights=="Unweighted"],
  means.graph$R7.se[means.graph$year==2021&means.graph$party_temp=="Democrat"&means.graph$weights=="Unweighted"],
  means.graph$R8.se[means.graph$year==2016&means.graph$party_temp=="Democrat"&means.graph$weights=="Unweighted"],
  means.graph$R8.se[means.graph$year==2021&means.graph$party_temp=="Democrat"&means.graph$weights=="Unweighted"],
  means.graph$D1.se[means.graph$year==2016&means.graph$party_temp=="Republican"&means.graph$weights=="Unweighted"],
  means.graph$D1.se[means.graph$year==2021&means.graph$party_temp=="Republican"&means.graph$weights=="Unweighted"],
  means.graph$D2.se[means.graph$year==2016&means.graph$party_temp=="Republican"&means.graph$weights=="Unweighted"],
  means.graph$D2.se[means.graph$year==2021&means.graph$party_temp=="Republican"&means.graph$weights=="Unweighted"],
  means.graph$D3.se[means.graph$year==2016&means.graph$party_temp=="Republican"&means.graph$weights=="Unweighted"],
  means.graph$D3.se[means.graph$year==2021&means.graph$party_temp=="Republican"&means.graph$weights=="Unweighted"],
  means.graph$D4.se[means.graph$year==2016&means.graph$party_temp=="Republican"&means.graph$weights=="Unweighted"],
  means.graph$D4.se[means.graph$year==2021&means.graph$party_temp=="Republican"&means.graph$weights=="Unweighted"],
  means.graph$D5.se[means.graph$year==2016&means.graph$party_temp=="Republican"&means.graph$weights=="Unweighted"],
  means.graph$D5.se[means.graph$year==2021&means.graph$party_temp=="Republican"&means.graph$weights=="Unweighted"],
  means.graph$D6.se[means.graph$year==2016&means.graph$party_temp=="Republican"&means.graph$weights=="Unweighted"],
  means.graph$D6.se[means.graph$year==2021&means.graph$party_temp=="Republican"&means.graph$weights=="Unweighted"],
  means.graph$D7.se[means.graph$year==2016&means.graph$party_temp=="Republican"&means.graph$weights=="Unweighted"],
  means.graph$D7.se[means.graph$year==2021&means.graph$party_temp=="Republican"&means.graph$weights=="Unweighted"],
  means.graph$D8.se[means.graph$year==2016&means.graph$party_temp=="Republican"&means.graph$weights=="Unweighted"],
  means.graph$D8.se[means.graph$year==2021&means.graph$party_temp=="Republican"&means.graph$weights=="Unweighted"],
  means.graph$D1.se[means.graph$year==2016&means.graph$party_temp=="Democrat"&means.graph$weights=="Unweighted"],
  means.graph$D1.se[means.graph$year==2021&means.graph$party_temp=="Democrat"&means.graph$weights=="Unweighted"],
  means.graph$D2.se[means.graph$year==2016&means.graph$party_temp=="Democrat"&means.graph$weights=="Unweighted"],
  means.graph$D2.se[means.graph$year==2021&means.graph$party_temp=="Democrat"&means.graph$weights=="Unweighted"],
  means.graph$D3.se[means.graph$year==2016&means.graph$party_temp=="Democrat"&means.graph$weights=="Unweighted"],
  means.graph$D3.se[means.graph$year==2021&means.graph$party_temp=="Democrat"&means.graph$weights=="Unweighted"],
  means.graph$D4.se[means.graph$year==2016&means.graph$party_temp=="Democrat"&means.graph$weights=="Unweighted"],
  means.graph$D4.se[means.graph$year==2021&means.graph$party_temp=="Democrat"&means.graph$weights=="Unweighted"],
  means.graph$D5.se[means.graph$year==2016&means.graph$party_temp=="Democrat"&means.graph$weights=="Unweighted"],
  means.graph$D5.se[means.graph$year==2021&means.graph$party_temp=="Democrat"&means.graph$weights=="Unweighted"],
  means.graph$D6.se[means.graph$year==2016&means.graph$party_temp=="Democrat"&means.graph$weights=="Unweighted"],
  means.graph$D6.se[means.graph$year==2021&means.graph$party_temp=="Democrat"&means.graph$weights=="Unweighted"],
  means.graph$D7.se[means.graph$year==2016&means.graph$party_temp=="Democrat"&means.graph$weights=="Unweighted"],
  means.graph$D7.se[means.graph$year==2021&means.graph$party_temp=="Democrat"&means.graph$weights=="Unweighted"],
  means.graph$D8.se[means.graph$year==2016&means.graph$party_temp=="Democrat"&means.graph$weights=="Unweighted"],
  means.graph$D8.se[means.graph$year==2021&means.graph$party_temp=="Democrat"&means.graph$weights=="Unweighted"]
)

year=c(rep(c(2016,2021),32)
)

#Add these together as one data object
t2=as.data.frame(cbind(party, respondent))
t2$topic=topic
t2$bar=bar
t2$year=year
t2$year=as.factor(year)
t2$se=se
t2$low=t2$bar-(1.96*t2$se)
t2$high=t2$bar+(1.96*t2$se)

##Graph it
bar2w=ggplot(data=t2[t2$party=="Stereotypes of Democrats"&t2$respondent=="Democratic respondents",], aes(x=topic, y=bar, fill=year, group=year))+
  geom_bar(position=position_dodge(), stat="identity")+
  geom_errorbar(aes(ymin=low, ymax=high),width=0.2, position=position_dodge((0.9)))+
  xlab("")+
  ylab("Democratic respondents")+
  ggtitle("")+
  theme(plot.caption=element_text(hjust=0))+
  scale_fill_grey()+
  scale_x_continuous(breaks = unique(t$topic),
                     labels=c("1. Bad People", "2. Nonresponse", "3. Liberal Coalition",
                              "4. Class Interests", "5. Dishonest and Lazy", "6. Smart and Honest",
                              "7. Insincere Positive", "8. Caring and Inclusive"))+
  scale_y_continuous(limits=c(-0.06,0.7))
bar2w=bar2w+coord_flip()+theme_bw()+theme(legend.position = "none")

bar2.1w=ggplot(data=t2[t2$party=="Stereotypes of Democrats"&t2$respondent=="Republican respondents",], aes(x=topic, y=bar, fill=year, group=year))+
  geom_bar(position=position_dodge(), stat="identity")+
  geom_errorbar(aes(ymin=low, ymax=high),width=0.2, position=position_dodge((0.9)))+
  xlab("")+
  ylab("Republican respondents")+
  ggtitle("")+
  theme(plot.caption=element_text(hjust=0))+
  scale_fill_grey()+
  scale_x_continuous(breaks = unique(t$topic),
                     labels=c("", "", "",
                              "", "", "",
                              "", ""))+
  scale_y_continuous(limits=c(-0.06,0.7))
bar2.1w=bar2.1w+coord_flip()+theme_bw()+
  theme(legend.position = "none")

bar2.2w=ggplot(data=t2[t2$party=="Stereotypes of Republicans"&t2$respondent=="Democratic respondents",], aes(x=topic, y=bar, fill=year, group=year))+
  geom_bar(position=position_dodge(), stat="identity")+
  geom_errorbar(aes(ymin=low, ymax=high),width=0.2, position=position_dodge((0.9)))+
  xlab("")+
  ylab("Democratic respondents")+
  ggtitle("")+
  theme(plot.caption=element_text(hjust=0))+
  scale_fill_grey()+
  scale_x_continuous(breaks = unique(t$topic),
                     labels=c("1. Nonresponse", "2. Caring Patriots", "3. Rich White Men",
                              "4. Selfish Bigots", "5. Traditional Coalition", "6. Class Interests",
                              "7. Religious \nFundamentalists", "8. Insincere Positive"))+
  scale_y_continuous(limits=c(-0.06,0.7))
bar2.2w=bar2.2w+coord_flip()+theme_bw()+
  theme(legend.position = "bottom",
        legend.key.size=unit(0.3, "cm"),
        legend.text=element_text(size=8),
        legend.margin=margin(0))+ theme(legend.title=element_blank())

bar2.3w=ggplot(data=t2[t2$party=="Stereotypes of Republicans"&t2$respondent=="Republican respondents",], aes(x=topic, y=bar, fill=year, group=year))+
  geom_bar(position=position_dodge(), stat="identity")+
  geom_errorbar(aes(ymin=low, ymax=high),width=0.2, position=position_dodge((0.9)))+
  xlab("")+
  ylab("Republican respondents")+
  ggtitle("")+
  labs(caption="Figure presents topic proportions along with 95 percent confidence intervals")+
  theme(plot.caption=element_text(hjust=0))+
  scale_fill_grey()+
  scale_x_continuous(breaks = unique(t$topic),
                     labels=c("", "", "",
                              "", "", "",
                              "", ""))+
  scale_y_continuous(limits=c(-0.06,0.7))
bar2.3w=bar2.3w+coord_flip()+theme_bw()+
  theme(legend.position = "none")

plot_grid(bar2w, bar2.1w, bar2.2w, bar2.3w, labels=c("Stereotypes of Democrats", "", "Stereotypes of Republicans", ""),
          rel_widths = c(1.1,0.9, 1.5, 1), rel_heights = c(1,1,1,0.85))

jpeg("Figure G1-unweighted-mean-no insincere.jpeg", width=10, height=8, units="in", res=600)
plot_grid(bar2w, bar2.1w, bar2.2w, bar2.3w, labels=c("Stereotypes of Democrats", "", "Stereotypes of Republicans", ""),
          rel_widths = c(1.1,0.9, 1.5, 1), rel_heights = c(1,1,1,0.85))
dev.off()
#The proportion giving the insincere responses topic diminishes by about 0.5-1%
mean(mergedD$Topic7, na.rm=T)
mean(mergedD4$Topic7, na.rm=T)
mean(mergedD$Topic7, na.rm=T)-mean(mergedD4$Topic7, na.rm=T)
mean(mergedR$Topic8, na.rm=T)
mean(mergedR4$Topic8, na.rm=T)
mean(mergedR$Topic8, na.rm=T)-mean(mergedR4$Topic8, na.rm=T)

#Look at other topics
mean(mergedD$Topic1, na.rm=T)-mean(mergedD4$Topic1, na.rm=T)
mean(mergedD$Topic2, na.rm=T)-mean(mergedD4$Topic2, na.rm=T)
mean(mergedD$Topic3, na.rm=T)-mean(mergedD4$Topic3, na.rm=T)
mean(mergedD$Topic4, na.rm=T)-mean(mergedD4$Topic4, na.rm=T)
mean(mergedD$Topic5, na.rm=T)-mean(mergedD4$Topic5, na.rm=T)
mean(mergedD$Topic6, na.rm=T)-mean(mergedD4$Topic6, na.rm=T)
mean(mergedD$Topic7, na.rm=T)-mean(mergedD4$Topic7, na.rm=T)
mean(mergedD$Topic8, na.rm=T)-mean(mergedD4$Topic8, na.rm=T)

mean(mergedR$Topic1, na.rm=T)-mean(mergedR4$Topic1, na.rm=T)
mean(mergedR$Topic2, na.rm=T)-mean(mergedR4$Topic2, na.rm=T)
mean(mergedR$Topic3, na.rm=T)-mean(mergedR4$Topic3, na.rm=T)
mean(mergedR$Topic4, na.rm=T)-mean(mergedR4$Topic4, na.rm=T)
mean(mergedR$Topic5, na.rm=T)-mean(mergedR4$Topic5, na.rm=T)
mean(mergedR$Topic6, na.rm=T)-mean(mergedR4$Topic6, na.rm=T)
mean(mergedR$Topic7, na.rm=T)-mean(mergedR4$Topic7, na.rm=T)
mean(mergedR$Topic8, na.rm=T)-mean(mergedR4$Topic8, na.rm=T)

####Appendix H:####
#Figure H.1
plot(GenDSearch_alt)
#Table H.1
knitr::kable(GenDSearch_alt$results)

#Figure H.2
plot(GenRSearch_alt)
#Table H.2
knitr::kable(GenRSearch_alt$results)

#Figure H.3
plot(GenDSearch2)
#Table H.3
knitr::kable(GenDSearch2$results)

#Figure H.4
plot(GenRSearch2)
#Table H.4
knitr::kable(GenRSearch2$results)

#Figure H.5
plotModels(GenDSelect5Sage)
plotModels(GenDSelect7Sage)
plotModels(GenDSelect8Sage)
plotModels(GenDSelect9Sage)
plotModels(GenDSelect10Sage)

#Figure H.6
plotModels(GenRSelect4Sage)
plotModels(GenRSelect7Sage)
plotModels(GenRSelect8Sage)
plotModels(GenRSelect9Sage)
plotModels(GenRSelect11Sage)

#Table H.5
#Five topic model
GenDChoice5.5 <- GenDSelect5Sage$runout[[5]]
summary(GenDChoice5.2)

GenDChoice5.5 <- GenDSelect5Sage$runout[[5]]
summary(GenDChoice5.5)

GenDChoice5.6 <- GenDSelect5Sage$runout[[6]]
summary(GenDChoice5.6)

#Seven topic model
GenDChoice7.2 <- GenDSelect7Sage$runout[[2]]
summary(GenDChoice7.2)

GenDChoice7.3 <- GenDSelect7Sage$runout[[3]]
summary(GenDChoice7.3)

GenDChoice7.5 <- GenDSelect7Sage$runout[[5]]
summary(GenDChoice7.5)

#Nine topic model
GenDChoice9.3 <- GenDSelect9Sage$runout[[3]]
summary(GenDChoice9.3)

GenDChoice9.5 <- GenDSelect9Sage$runout[[5]]
summary(GenDChoice9.5)

#Ten topic model
GenDChoice10.1 <- GenDSelect10Sage$runout[[1]]
summary(GenDChoice10.1)

#Table H.6
#Four topic model
GenRChoice4.2 <- GenRSelect4Sage$runout[[2]]
summary(GenRChoice4.2)

GenRChoice4.6 <- GenRSelect4Sage$runout[[6]]
summary(GenRChoice4.6)

#Seven topic model
GenRChoice7.2 <- GenRSelect7Sage$runout[[2]]
summary(GenRChoice7.2)

GenRChoice7.3 <- GenRSelect7Sage$runout[[3]]
summary(GenRChoice7.3)

GenRChoice7.5 <- GenRSelect7Sage$runout[[5]]
summary(GenRChoice7.5)

#Eight topic model
GenRChoice8.5 <- GenRSelect8Sage$runout[[5]]
summary(GenRChoice8.5)

GenRChoice8.1 <- GenRSelect8Sage$runout[[1]]
summary(GenRChoice8.1)

#Nine topic model
GenRChoice9.5 <- GenRSelect9Sage$runout[[5]]
summary(GenRChoice9.5)

#eleven topic model
GenRChoice11.1 <- GenRSelect11Sage$runout[[1]]
summary(GenRChoice11.1)

GenRChoice11.2 <- GenRSelect11Sage$runout[[2]]
summary(GenRChoice11.2)

GenRChoice11.3 <- GenRSelect11Sage$runout[[3]]
summary(GenRChoice11.3)

####Close log file####
#Print R version and package versions:
print(sessionInfo(), locale=F)
txtStop()